addressComponent.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. 'use strict';
  2. import BaseComponent from './baseComponent'
  3. /*
  4. 腾讯地图和百度地图API统一调配组件
  5. */
  6. class AddressComponent extends BaseComponent {
  7. constructor(){
  8. super();
  9. this.tencentkey = 'RLHBZ-WMPRP-Q3JDS-V2IQA-JNRFH-EJBHL';
  10. this.baidukey = 'fjke3YUipM9N64GdOIh1DNeK2APO2WcT';
  11. }
  12. //获取定位地址
  13. async guessPosition(req){
  14. return new Promise(async (resolve, reject) => {
  15. let ip = req.headers['x-forwarded-for'] ||
  16. req.connection.remoteAddress ||
  17. req.socket.remoteAddress ||
  18. req.connection.socket.remoteAddress;
  19. const ipArr = ip.split(':');
  20. ip = ipArr[ipArr.length -1];
  21. if (ip.indexOf('.') == -1) {
  22. resolve({
  23. city: '本地访问'
  24. })
  25. return
  26. }
  27. try{
  28. const result = await this.fetch('http://apis.map.qq.com/ws/location/v1/ip', {
  29. ip,
  30. key: this.tencentkey,
  31. })
  32. if (result.status == 0) {
  33. const cityInfo = {
  34. lat: result.result.location.lat,
  35. lng: result.result.location.lng,
  36. city: result.result.ad_info.city,
  37. }
  38. cityInfo.city = cityInfo.city.replace(/市$/, '');
  39. resolve(cityInfo)
  40. }else{
  41. reject('定位失败');
  42. }
  43. }catch(err){
  44. reject(err);
  45. }
  46. })
  47. }
  48. //搜索地址
  49. async searchPlace(keyword, cityName, type = 'search'){
  50. try{
  51. const resObj = await this.fetch('http://apis.map.qq.com/ws/place/v1/search', {
  52. key: this.tencentkey,
  53. keyword: encodeURIComponent(keyword),
  54. boundary: 'region(' + encodeURIComponent(cityName) + ',0)',
  55. page_size: 10,
  56. });
  57. if (resObj.status == 0) {
  58. return resObj
  59. }else{
  60. throw new Error('搜索位置信息失败');
  61. }
  62. }catch(err){
  63. throw new Error(err);
  64. }
  65. }
  66. //测量距离
  67. async getDistance(from, to, type){
  68. try{
  69. const res = await this.fetch('http://api.map.baidu.com/routematrix/v2/driving', {
  70. ak: this.baidukey,
  71. output: 'json',
  72. origins: from,
  73. destinations: to,
  74. })
  75. if(res.status == 0){
  76. const positionArr = [];
  77. let timevalue;
  78. res.result.forEach(item => {
  79. timevalue = parseInt(item.duration.value) + 1200;
  80. let durationtime = Math.ceil(timevalue%3600/60) + '分钟';
  81. if(Math.floor(timevalue/3600)){
  82. durationtime = Math.floor(timevalue/3600) + '小时' + durationtime;
  83. }
  84. positionArr.push({
  85. distance: item.distance.text,
  86. order_lead_time: durationtime,
  87. })
  88. })
  89. if (type == 'tiemvalue') {
  90. return timevalue
  91. }else{
  92. return positionArr
  93. }
  94. }else{
  95. throw new Error('调用百度地图测距失败');
  96. }
  97. }catch(err){
  98. console.log('获取位置距离失败')
  99. throw new Error(err);
  100. }
  101. }
  102. //通过ip地址获取精确位置
  103. async geocoder(req){
  104. try{
  105. const address = await this.guessPosition(req);
  106. const res = await this.fetch('http://apis.map.qq.com/ws/geocoder/v1/', {
  107. key: this.tencentkey,
  108. location: address.lat + ',' + address.lng
  109. })
  110. if (res.status == 0) {
  111. return res
  112. }else{
  113. throw new Error('获取具体位置信息失败');
  114. }
  115. }catch(err){
  116. console.log('geocoder获取定位失败')
  117. throw new Error(err);
  118. }
  119. }
  120. //通过geohash获取精确位置
  121. async getpois(lat, lng){
  122. try{
  123. const res = await this.fetch('http://apis.map.qq.com/ws/geocoder/v1/', {
  124. key: this.tencentkey,
  125. location: lat + ',' + lng
  126. })
  127. if (res.status == 0) {
  128. return res
  129. }else{
  130. throw new Error('通过获geohash取具体位置失败');
  131. }
  132. }catch(err){
  133. console.log('getpois获取定位失败')
  134. throw new Error(err);
  135. }
  136. }
  137. }
  138. export default AddressComponent