addressComponent.js 3.4 KB

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