loc.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. const QQMapWX = require('./qqmap-wx-jssdk.min.js')
  2. // key: 'QNVBZ-FOQK3-ZOT3S-3GADS-7KAG3-LBBQB' //测即成key
  3. const qqmapsdk = new QQMapWX({
  4. key: 'RXSBZ-GUOYJ-OZOFI-FBMOP-3CMF3-D6FGE' //hjt
  5. })
  6. //逆向解析
  7. function getNameByLatLng(latitude,longitude){
  8. return new Promise((resolve,reject)=>{
  9. qqmapsdk.reverseGeocoder({
  10. location: {latitude,longitude},
  11. success: res=> {
  12. // console.log("getNameByLatLng--",res.result)
  13. resolve(res.result);
  14. },
  15. fail: err=> {
  16. reject("逆向地址解析错误");
  17. console.log(err)
  18. }
  19. })
  20. })
  21. }
  22. async function chooseLoc(){
  23. return new Promise(async (resolve,reject)=>{
  24. let [cErr,cRes] = await uni.chooseLocation({type:'gcj02'});
  25. if(cErr){
  26. reject(cErr)
  27. return;
  28. }
  29. let {latitude,longitude} = cRes
  30. let loc = await getNameByLatLng(latitude,longitude) || {};
  31. // console.log('getNameByLatLng===',loc);
  32. if( loc.address_component){
  33. let {province,city,district} = loc.address_component;
  34. let {lat,lng} = loc.location;
  35. let desc = loc.formatted_addresses.recommend;
  36. resolve({
  37. province,
  38. city,
  39. district,
  40. desc,
  41. latitude:lat,
  42. longitude:lng,
  43. })
  44. return;
  45. }
  46. return reject(false);
  47. })
  48. }
  49. export{
  50. chooseLoc,
  51. }