addressComponent.js 3.4 KB

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