当前位置:   article > 正文

uniapp 微信小程序使用高德api获取用户位置信息_amap-wx

amap-wx

1.进入高德开放平台创建应用https://console.amap.com/dev/key/app

2.添加amap-wx.130.js文件

配置工程-创建工程-开发指南-微信小程序插件 | 高德地图API

上面这个链接下载的 amap-wx.130.js文件直接使用会报错  需要转成es6的方式 如图片下面的代码可以直接使用

  1. function AMapWX(a) {
  2. this.key = a.key;
  3. this.requestConfig = {
  4. key: a.key,
  5. s: "rsx",
  6. platform: "WXJS",
  7. appname: a.key,
  8. sdkversion: "1.2.0",
  9. logversion: "2.0"
  10. };
  11. this.MeRequestConfig = {key: a.key, serviceName: "https://restapi.amap.com/rest/me"}
  12. }
  13. AMapWX.prototype.getWxLocation = function (a, b) {
  14. wx.getLocation({
  15. type: "gcj02", success: function (c) {
  16. c = c.longitude + "," + c.latitude;
  17. wx.setStorage({key: "userLocation", data: c});
  18. b(c)
  19. }, fail: function (c) {
  20. wx.getStorage({
  21. key: "userLocation", success: function (d) {
  22. d.data && b(d.data)
  23. }
  24. });
  25. a.fail({errCode: "0", errMsg: c.errMsg || ""})
  26. }
  27. })
  28. };
  29. AMapWX.prototype.getMEKeywordsSearch = function (a) {
  30. if (!a.options) return a.fail({errCode: "0", errMsg: "\u7f3a\u5c11\u5fc5\u8981\u53c2\u6570"});
  31. var b = a.options, c = this.MeRequestConfig,
  32. d = {key: c.key, s: "rsx", platform: "WXJS", appname: a.key, sdkversion: "1.2.0", logversion: "2.0"};
  33. b.layerId && (d.layerId = b.layerId);
  34. b.keywords && (d.keywords = b.keywords);
  35. b.city && (d.city = b.city);
  36. b.filter && (d.filter = b.filter);
  37. b.sortrule && (d.sortrule = b.sortrule);
  38. b.pageNum && (d.pageNum = b.pageNum);
  39. b.pageSize && (d.pageSize = b.pageSize);
  40. b.sig && (d.sig =
  41. b.sig);
  42. wx.request({
  43. url: c.serviceName + "/cpoint/datasearch/local",
  44. data: d,
  45. method: "GET",
  46. header: {"content-type": "application/json"},
  47. success: function (e) {
  48. (e = e.data) && e.status && "1" === e.status && 0 === e.code ? a.success(e.data) : a.fail({
  49. errCode: "0",
  50. errMsg: e
  51. })
  52. },
  53. fail: function (e) {
  54. a.fail({errCode: "0", errMsg: e.errMsg || ""})
  55. }
  56. })
  57. };
  58. AMapWX.prototype.getMEIdSearch = function (a) {
  59. if (!a.options) return a.fail({errCode: "0", errMsg: "\u7f3a\u5c11\u5fc5\u8981\u53c2\u6570"});
  60. var b = a.options, c = this.MeRequestConfig,
  61. d = {key: c.key, s: "rsx", platform: "WXJS", appname: a.key, sdkversion: "1.2.0", logversion: "2.0"};
  62. b.layerId && (d.layerId = b.layerId);
  63. b.id && (d.id = b.id);
  64. b.sig && (d.sig = b.sig);
  65. wx.request({
  66. url: c.serviceName + "/cpoint/datasearch/id",
  67. data: d,
  68. method: "GET",
  69. header: {"content-type": "application/json"},
  70. success: function (e) {
  71. (e = e.data) && e.status && "1" === e.status &&
  72. 0 === e.code ? a.success(e.data) : a.fail({errCode: "0", errMsg: e})
  73. },
  74. fail: function (e) {
  75. a.fail({errCode: "0", errMsg: e.errMsg || ""})
  76. }
  77. })
  78. };
  79. AMapWX.prototype.getMEPolygonSearch = function (a) {
  80. if (!a.options) return a.fail({errCode: "0", errMsg: "\u7f3a\u5c11\u5fc5\u8981\u53c2\u6570"});
  81. var b = a.options, c = this.MeRequestConfig,
  82. d = {key: c.key, s: "rsx", platform: "WXJS", appname: a.key, sdkversion: "1.2.0", logversion: "2.0"};
  83. b.layerId && (d.layerId = b.layerId);
  84. b.keywords && (d.keywords = b.keywords);
  85. b.polygon && (d.polygon = b.polygon);
  86. b.filter && (d.filter = b.filter);
  87. b.sortrule && (d.sortrule = b.sortrule);
  88. b.pageNum && (d.pageNum = b.pageNum);
  89. b.pageSize && (d.pageSize = b.pageSize);
  90. b.sig && (d.sig = b.sig);
  91. wx.request({
  92. url: c.serviceName + "/cpoint/datasearch/polygon",
  93. data: d,
  94. method: "GET",
  95. header: {"content-type": "application/json"},
  96. success: function (e) {
  97. (e = e.data) && e.status && "1" === e.status && 0 === e.code ? a.success(e.data) : a.fail({
  98. errCode: "0",
  99. errMsg: e
  100. })
  101. },
  102. fail: function (e) {
  103. a.fail({errCode: "0", errMsg: e.errMsg || ""})
  104. }
  105. })
  106. };
  107. AMapWX.prototype.getMEaroundSearch = function (a) {
  108. if (!a.options) return a.fail({errCode: "0", errMsg: "\u7f3a\u5c11\u5fc5\u8981\u53c2\u6570"});
  109. var b = a.options, c = this.MeRequestConfig,
  110. d = {key: c.key, s: "rsx", platform: "WXJS", appname: a.key, sdkversion: "1.2.0", logversion: "2.0"};
  111. b.layerId && (d.layerId = b.layerId);
  112. b.keywords && (d.keywords = b.keywords);
  113. b.center && (d.center = b.center);
  114. b.radius && (d.radius = b.radius);
  115. b.filter && (d.filter = b.filter);
  116. b.sortrule && (d.sortrule = b.sortrule);
  117. b.pageNum && (d.pageNum = b.pageNum);
  118. b.pageSize &&
  119. (d.pageSize = b.pageSize);
  120. b.sig && (d.sig = b.sig);
  121. wx.request({
  122. url: c.serviceName + "/cpoint/datasearch/around",
  123. data: d,
  124. method: "GET",
  125. header: {"content-type": "application/json"},
  126. success: function (e) {
  127. (e = e.data) && e.status && "1" === e.status && 0 === e.code ? a.success(e.data) : a.fail({
  128. errCode: "0",
  129. errMsg: e
  130. })
  131. },
  132. fail: function (e) {
  133. a.fail({errCode: "0", errMsg: e.errMsg || ""})
  134. }
  135. })
  136. };
  137. AMapWX.prototype.getGeo = function (a) {
  138. var b = this.requestConfig, c = a.options;
  139. b = {
  140. key: this.key,
  141. extensions: "all",
  142. s: b.s,
  143. platform: b.platform,
  144. appname: this.key,
  145. sdkversion: b.sdkversion,
  146. logversion: b.logversion
  147. };
  148. c.address && (b.address = c.address);
  149. c.city && (b.city = c.city);
  150. c.batch && (b.batch = c.batch);
  151. c.sig && (b.sig = c.sig);
  152. wx.request({
  153. url: "https://restapi.amap.com/v3/geocode/geo",
  154. data: b,
  155. method: "GET",
  156. header: {"content-type": "application/json"},
  157. success: function (d) {
  158. (d = d.data) && d.status && "1" === d.status ? a.success(d) : a.fail({
  159. errCode: "0",
  160. errMsg: d
  161. })
  162. },
  163. fail: function (d) {
  164. a.fail({errCode: "0", errMsg: d.errMsg || ""})
  165. }
  166. })
  167. };
  168. AMapWX.prototype.getRegeo = function (a) {
  169. function b(d) {
  170. var e = c.requestConfig;
  171. wx.request({
  172. url: "https://restapi.amap.com/v3/geocode/regeo",
  173. data: {
  174. key: c.key,
  175. location: d,
  176. extensions: "all",
  177. s: e.s,
  178. platform: e.platform,
  179. appname: c.key,
  180. sdkversion: e.sdkversion,
  181. logversion: e.logversion
  182. },
  183. method: "GET",
  184. header: {"content-type": "application/json"},
  185. success: function (g) {
  186. if (g.data.status && "1" == g.data.status) {
  187. g = g.data.regeocode;
  188. var h = g.addressComponent, f = [], k = g.roads[0].name + "\u9644\u8fd1", m = d.split(",")[0],
  189. n = d.split(",")[1];
  190. if (g.pois &&
  191. g.pois[0]) {
  192. k = g.pois[0].name + "\u9644\u8fd1";
  193. var l = g.pois[0].location;
  194. l && (m = parseFloat(l.split(",")[0]), n = parseFloat(l.split(",")[1]))
  195. }
  196. h.provice && f.push(h.provice);
  197. h.city && f.push(h.city);
  198. h.district && f.push(h.district);
  199. h.streetNumber && h.streetNumber.street && h.streetNumber.number ? (f.push(h.streetNumber.street), f.push(h.streetNumber.number)) : f.push(g.roads[0].name);
  200. f = f.join("");
  201. a.success([{
  202. iconPath: a.iconPath,
  203. width: a.iconWidth,
  204. height: a.iconHeight,
  205. name: f,
  206. desc: k,
  207. longitude: m,
  208. latitude: n,
  209. id: 0,
  210. regeocodeData: g
  211. }])
  212. } else a.fail({
  213. errCode: g.data.infocode,
  214. errMsg: g.data.info
  215. })
  216. },
  217. fail: function (g) {
  218. a.fail({errCode: "0", errMsg: g.errMsg || ""})
  219. }
  220. })
  221. }
  222. var c = this;
  223. a.location ? b(a.location) : c.getWxLocation(a, function (d) {
  224. b(d)
  225. })
  226. };
  227. AMapWX.prototype.getWeather = function (a) {
  228. function b(g) {
  229. var h = "base";
  230. a.type && "forecast" == a.type && (h = "all");
  231. wx.request({
  232. url: "https://restapi.amap.com/v3/weather/weatherInfo",
  233. data: {
  234. key: d.key,
  235. city: g,
  236. extensions: h,
  237. s: e.s,
  238. platform: e.platform,
  239. appname: d.key,
  240. sdkversion: e.sdkversion,
  241. logversion: e.logversion
  242. },
  243. method: "GET",
  244. header: {"content-type": "application/json"},
  245. success: function (f) {
  246. if (f.data.status && "1" == f.data.status) if (f.data.lives) {
  247. if ((f = f.data.lives) && 0 < f.length) {
  248. f = f[0];
  249. var k = {
  250. city: {text: "\u57ce\u5e02", data: f.city},
  251. weather: {text: "\u5929\u6c14", data: f.weather},
  252. temperature: {text: "\u6e29\u5ea6", data: f.temperature},
  253. winddirection: {text: "\u98ce\u5411", data: f.winddirection + "\u98ce"},
  254. windpower: {text: "\u98ce\u529b", data: f.windpower + "\u7ea7"},
  255. humidity: {text: "\u6e7f\u5ea6", data: f.humidity + "%"}
  256. };
  257. k.liveData = f;
  258. a.success(k)
  259. }
  260. } else f.data.forecasts && f.data.forecasts[0] && a.success({forecast: f.data.forecasts[0]}); else a.fail({
  261. errCode: f.data.infocode,
  262. errMsg: f.data.info
  263. })
  264. },
  265. fail: function (f) {
  266. a.fail({errCode: "0", errMsg: f.errMsg || ""})
  267. }
  268. })
  269. }
  270. function c(g) {
  271. wx.request({
  272. url: "https://restapi.amap.com/v3/geocode/regeo",
  273. data: {
  274. key: d.key,
  275. location: g,
  276. extensions: "all",
  277. s: e.s,
  278. platform: e.platform,
  279. appname: d.key,
  280. sdkversion: e.sdkversion,
  281. logversion: e.logversion
  282. },
  283. method: "GET",
  284. header: {"content-type": "application/json"},
  285. success: function (h) {
  286. if (h.data.status && "1" == h.data.status) {
  287. h = h.data.regeocode;
  288. if (h.addressComponent) var f = h.addressComponent.adcode; else h.aois && 0 < h.aois.length && (f = h.aois[0].adcode);
  289. b(f)
  290. } else a.fail({errCode: h.data.infocode, errMsg: h.data.info})
  291. },
  292. fail: function (h) {
  293. a.fail({errCode: "0", errMsg: h.errMsg || ""})
  294. }
  295. })
  296. }
  297. var d = this, e = d.requestConfig;
  298. a.city ? b(a.city) : d.getWxLocation(a, function (g) {
  299. c(g)
  300. })
  301. };
  302. AMapWX.prototype.getPoiAround = function (a) {
  303. function b(e) {
  304. e = {
  305. key: c.key,
  306. location: e,
  307. s: d.s,
  308. platform: d.platform,
  309. appname: c.key,
  310. sdkversion: d.sdkversion,
  311. logversion: d.logversion
  312. };
  313. a.querytypes && (e.types = a.querytypes);
  314. a.querykeywords && (e.keywords = a.querykeywords);
  315. wx.request({
  316. url: "https://restapi.amap.com/v3/place/around",
  317. data: e,
  318. method: "GET",
  319. header: {"content-type": "application/json"},
  320. success: function (g) {
  321. if (g.data.status && "1" == g.data.status) {
  322. if ((g = g.data) && g.pois) {
  323. for (var h = [], f = 0; f < g.pois.length; f++) {
  324. var k = 0 ==
  325. f ? a.iconPathSelected : a.iconPath;
  326. h.push({
  327. latitude: parseFloat(g.pois[f].location.split(",")[1]),
  328. longitude: parseFloat(g.pois[f].location.split(",")[0]),
  329. iconPath: k,
  330. width: 22,
  331. height: 32,
  332. id: f,
  333. name: g.pois[f].name,
  334. address: g.pois[f].address
  335. })
  336. }
  337. a.success({markers: h, poisData: g.pois})
  338. }
  339. } else a.fail({errCode: g.data.infocode, errMsg: g.data.info})
  340. },
  341. fail: function (g) {
  342. a.fail({errCode: "0", errMsg: g.errMsg || ""})
  343. }
  344. })
  345. }
  346. var c = this, d = c.requestConfig;
  347. a.location ? b(a.location) : c.getWxLocation(a, function (e) {
  348. b(e)
  349. })
  350. };
  351. AMapWX.prototype.getStaticmap = function (a) {
  352. function b(e) {
  353. c.push("location=" + e);
  354. a.zoom && c.push("zoom=" + a.zoom);
  355. a.size && c.push("size=" + a.size);
  356. a.scale && c.push("scale=" + a.scale);
  357. a.markers && c.push("markers=" + a.markers);
  358. a.labels && c.push("labels=" + a.labels);
  359. a.paths && c.push("paths=" + a.paths);
  360. a.traffic && c.push("traffic=" + a.traffic);
  361. e = "https://restapi.amap.com/v3/staticmap?" + c.join("&");
  362. a.success({url: e})
  363. }
  364. var c = [];
  365. c.push("key=" + this.key);
  366. var d = this.requestConfig;
  367. c.push("s=" + d.s);
  368. c.push("platform=" + d.platform);
  369. c.push("appname=" + d.appname);
  370. c.push("sdkversion=" + d.sdkversion);
  371. c.push("logversion=" + d.logversion);
  372. a.location ? b(a.location) : this.getWxLocation(a, function (e) {
  373. b(e)
  374. })
  375. };
  376. AMapWX.prototype.getInputtips = function (a) {
  377. var b = Object.assign({}, this.requestConfig);
  378. a.location && (b.location = a.location);
  379. a.keywords && (b.keywords = a.keywords);
  380. a.type && (b.type = a.type);
  381. a.city && (b.city = a.city);
  382. a.citylimit && (b.citylimit = a.citylimit);
  383. wx.request({
  384. url: "https://restapi.amap.com/v3/assistant/inputtips",
  385. data: b,
  386. method: "GET",
  387. header: {"content-type": "application/json"},
  388. success: function (c) {
  389. c && c.data && c.data.tips && a.success({tips: c.data.tips})
  390. },
  391. fail: function (c) {
  392. a.fail({
  393. errCode: "0", errMsg: c.errMsg ||
  394. ""
  395. })
  396. }
  397. })
  398. };
  399. AMapWX.prototype.getDrivingRoute = function (a) {
  400. var b = Object.assign({}, this.requestConfig);
  401. a.origin && (b.origin = a.origin);
  402. a.destination && (b.destination = a.destination);
  403. a.strategy && (b.strategy = a.strategy);
  404. a.waypoints && (b.waypoints = a.waypoints);
  405. a.avoidpolygons && (b.avoidpolygons = a.avoidpolygons);
  406. a.avoidroad && (b.avoidroad = a.avoidroad);
  407. wx.request({
  408. url: "https://restapi.amap.com/v3/direction/driving",
  409. data: b,
  410. method: "GET",
  411. header: {"content-type": "application/json"},
  412. success: function (c) {
  413. c && c.data && c.data.route && a.success({
  414. paths: c.data.route.paths,
  415. taxi_cost: c.data.route.taxi_cost || ""
  416. })
  417. },
  418. fail: function (c) {
  419. a.fail({errCode: "0", errMsg: c.errMsg || ""})
  420. }
  421. })
  422. };
  423. AMapWX.prototype.getWalkingRoute = function (a) {
  424. var b = Object.assign({}, this.requestConfig);
  425. a.origin && (b.origin = a.origin);
  426. a.destination && (b.destination = a.destination);
  427. wx.request({
  428. url: "https://restapi.amap.com/v3/direction/walking",
  429. data: b,
  430. method: "GET",
  431. header: {"content-type": "application/json"},
  432. success: function (c) {
  433. c && c.data && c.data.route && a.success({paths: c.data.route.paths})
  434. },
  435. fail: function (c) {
  436. a.fail({errCode: "0", errMsg: c.errMsg || ""})
  437. }
  438. })
  439. };
  440. AMapWX.prototype.getTransitRoute = function (a) {
  441. var b = Object.assign({}, this.requestConfig);
  442. a.origin && (b.origin = a.origin);
  443. a.destination && (b.destination = a.destination);
  444. a.strategy && (b.strategy = a.strategy);
  445. a.city && (b.city = a.city);
  446. a.cityd && (b.cityd = a.cityd);
  447. wx.request({
  448. url: "https://restapi.amap.com/v3/direction/transit/integrated",
  449. data: b,
  450. method: "GET",
  451. header: {"content-type": "application/json"},
  452. success: function (c) {
  453. c && c.data && c.data.route && (c = c.data.route, a.success({
  454. distance: c.distance || "", taxi_cost: c.taxi_cost ||
  455. "", transits: c.transits
  456. }))
  457. },
  458. fail: function (c) {
  459. a.fail({errCode: "0", errMsg: c.errMsg || ""})
  460. }
  461. })
  462. };
  463. AMapWX.prototype.getRidingRoute = function (a) {
  464. var b = Object.assign({}, this.requestConfig);
  465. a.origin && (b.origin = a.origin);
  466. a.destination && (b.destination = a.destination);
  467. wx.request({
  468. url: "https://restapi.amap.com/v3/direction/riding",
  469. data: b,
  470. method: "GET",
  471. header: {"content-type": "application/json"},
  472. success: function (c) {
  473. c && c.data && c.data.route && a.success({paths: c.data.route.paths})
  474. },
  475. fail: function (c) {
  476. a.fail({errCode: "0", errMsg: c.errMsg || ""})
  477. }
  478. })
  479. };
  480. // module.exports.AMapWX = AMapWX;
  481. export default { AMapWX };

3.config目录下

  1. export default {
  2. gdWxKey: '**********',//高德微信key
  3. }

4.封装方法

  1. /**
  2. * 获取用户当前位置
  3. */
  4. export function xxzGdUtilsGetRegeo() {
  5. return new Promise((resolve, reject) => {
  6. uni.getSetting({
  7. success(res) {
  8. if (!res.authSetting['scope.userLocation']) {
  9. // 位置授权未选择,调起授权
  10. uni.authorize({
  11. scope: 'scope.userLocation',
  12. success() {
  13. // 用户已同意授权位置信息
  14. const amapObject = new amap.AMapWX({key: config.gdWxKey})
  15. amapObject.getRegeo({
  16. success: res => resolve(res[0]),
  17. fail: err => reject(err)
  18. });
  19. },
  20. fail() {
  21. // 用户拒绝授权
  22. uni.showModal({
  23. title: '提示',
  24. content: '此功能需要获取您的位置信息,请确认授权',
  25. success: function (modalRes) {
  26. if (modalRes.confirm) {
  27. // 再次调起授权
  28. uni.openSetting();
  29. } else if (modalRes.cancel) {
  30. uni.switchTab({
  31. url: '/tabbar/home/index',
  32. })
  33. }
  34. }
  35. });
  36. }
  37. });
  38. } else {
  39. const amapObject = new amap.AMapWX({key: config.gdWxKey})
  40. amapObject.getRegeo({
  41. success: res => resolve(res[0]),
  42. fail: err => reject(err)
  43. });
  44. }
  45. }
  46. });
  47. });
  48. }

5.页面中使用

  1. /**
  2. * 通过高德API获取用户位置
  3. */
  4. const getUserLocation = async () => {
  5. const res: any = await xxzGdUtilsGetRegeo()
  6. console.log('高德地图',res)
  7. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/968008
推荐阅读
相关标签
  

闽ICP备14008679号