fun.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. console.log(result)
  43. uni.hideLoading();
  44. resolve(result)
  45. } else {
  46. uni.hideLoading();
  47. uni.showToast({
  48. title: '定位失败',
  49. });
  50. reject(result)
  51. }
  52. });
  53. });
  54. // #endif
  55. //h5结束
  56. //app开始
  57. // #ifdef APP-PLUS
  58. uni.showLoading({
  59. title: '获取信息中'
  60. });
  61. uni.getLocation({
  62. // map组件默认为国测局坐标gcj02,调用 uni.getLocation返回结果传递给组件时,需指定 type 为 gcj02
  63. type: 'gcj02',
  64. geocode: true,
  65. success: function(data) {
  66. resolve(data)
  67. console.log(data)
  68. },
  69. fail: function(err) {
  70. reject(err)
  71. },
  72. complete() {
  73. uni.hideLoading();
  74. }
  75. })
  76. // #endif
  77. //app结束
  78. ///小程序开始
  79. // #ifdef MP
  80. var amapPlugin = new amap.AMapWX({
  81. key: '9054f24276297df42fd1b6044f91abc5' //此处为高德平台申请的微信小程序的key
  82. });
  83. uni.showLoading({
  84. title: '获取信息中'
  85. });
  86. amapPlugin.getRegeo({
  87. success: function(data) {
  88. resolve(data)
  89. },
  90. fail: function(err) {
  91. reject(err)
  92. },
  93. complete: function() {
  94. uni.hideLoading();
  95. }
  96. });
  97. // #endif
  98. //小程序结束
  99. })
  100. };
  101. export default {
  102. getlocation: getlocation
  103. }