addressComponent.js 3.5 KB

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