fun.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //h5 要调用的js文件
  2. // 这是获取当前用户位置的方法,已经写好了
  3. /* 只需要调用下面函数就能获取坐标
  4. async init() {
  5. var t = this;
  6. t.$.getlocation()
  7. .then(res => {
  8. console.log(res, 'res');
  9. })
  10. .catch(err => {
  11. console.log(err, 'err');
  12. });
  13. },
  14. */
  15. // #ifdef H5
  16. import amap from '@/utils/maps.js';
  17. // #endif
  18. //微信小程序要调用的js文件
  19. // #ifdef MP
  20. import amap from '@/utils/amap-wx.js';
  21. // #endif
  22. //获取位置信息
  23. const getlocation = (opt) => {
  24. return new Promise((resolve, reject) => {
  25. //h5开始
  26. // #ifdef H5
  27. AMap.plugin('AMap.Geolocation', function() {
  28. uni.showLoading({
  29. title: '系统正在定位'
  30. });
  31. var geolocation = new AMap.Geolocation({
  32. enableHighAccuracy: true, //是否使用高精度定位,默认:true
  33. timeout: 5000, //超过10秒后停止定位,默认:5s
  34. buttonPosition: 'RB', //定位按钮的停靠位置
  35. buttonOffset: new AMap.Pixel(10, 20), //定位按钮与设置的停靠位置的偏移量,默认:Pixel(10, 20)
  36. zoomToAccuracy: true, //定位成功后是否自动调整地图视野到定位点
  37. });
  38. geolocation.getCurrentPosition(function(status, result) {
  39. if (status == 'complete') {
  40. //这个地方的result,可能会出现报错:获得地理定位时间。得到ipLocation成功。获取地址失败,请检查您的密钥或网络。
  41. //可能是密匙申请错了,重新申请密匙,生成maps.js文件。
  42. uni.hideLoading();
  43. resolve(result)
  44. } else {
  45. uni.hideLoading();
  46. uni.showToast({
  47. title: '定位失败',
  48. });
  49. reject(result)
  50. }
  51. });
  52. });
  53. // #endif
  54. //h5结束
  55. //app开始
  56. // #ifdef APP-PLUS
  57. uni.showLoading({
  58. title: '获取信息中'
  59. });
  60. uni.getLocation({
  61. // map组件默认为国测局坐标gcj02,调用 uni.getLocation返回结果传递给组件时,需指定 type 为 gcj02
  62. type: 'gcj02',
  63. geocode: true,
  64. success: function(data) {
  65. resolve(data)
  66. },
  67. fail: function(err) {
  68. reject(err)
  69. },
  70. complete() {
  71. uni.hideLoading();
  72. }
  73. })
  74. // #endif
  75. //app结束
  76. ///小程序开始
  77. // #ifdef MP
  78. var amapPlugin = new amap.AMapWX({
  79. key: '9054f24276297df42fd1b6044f91abc5' //此处为高德平台申请的微信小程序的key
  80. });
  81. uni.showLoading({
  82. title: '获取信息中'
  83. });
  84. amapPlugin.getRegeo({
  85. success: function(data) {
  86. resolve(data)
  87. },
  88. fail: function(err) {
  89. reject(err)
  90. },
  91. complete: function() {
  92. uni.hideLoading();
  93. }
  94. });
  95. // #endif
  96. //小程序结束
  97. })
  98. };
  99. export default {
  100. getlocation: getlocation
  101. }