123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- //h5 要调用的js文件
- // 这是获取当前用户位置的方法,已经写好了
- /* 只需要调用下面函数就能获取坐标
- async init() {
- var t = this;
- t.$.getlocation()
- .then(res => {
- console.log(res, 'res');
- })
- .catch(err => {
- console.log(err, 'err');
- });
- },
- */
- // #ifdef H5
- import amap from '@/utils/maps.js';
- // #endif
- //微信小程序要调用的js文件
- // #ifdef MP
- import amap from '@/utils/amap-wx.js';
- // #endif
- //获取位置信息
- const getlocation = (opt) => {
- return new Promise((resolve, reject) => {
- //h5开始
- // #ifdef H5
- AMap.plugin('AMap.Geolocation', function() {
- uni.showLoading({
- title: '系统正在定位'
- });
- var geolocation = new AMap.Geolocation({
- enableHighAccuracy: true, //是否使用高精度定位,默认:true
- timeout: 5000, //超过10秒后停止定位,默认:5s
- buttonPosition: 'RB', //定位按钮的停靠位置
- buttonOffset: new AMap.Pixel(10, 20), //定位按钮与设置的停靠位置的偏移量,默认:Pixel(10, 20)
- zoomToAccuracy: true, //定位成功后是否自动调整地图视野到定位点
- });
- geolocation.getCurrentPosition(function(status, result) {
- if (status == 'complete') {
- //这个地方的result,可能会出现报错:获得地理定位时间。得到ipLocation成功。获取地址失败,请检查您的密钥或网络。
- //可能是密匙申请错了,重新申请密匙,生成maps.js文件。
- uni.hideLoading();
- resolve(result)
- } else {
- uni.hideLoading();
- uni.showToast({
- title: '定位失败',
- });
- reject(result)
- }
- });
- });
- // #endif
- //h5结束
-
- //app开始
- // #ifdef APP-PLUS
- uni.showLoading({
- title: '获取信息中'
- });
- uni.getLocation({
- // map组件默认为国测局坐标gcj02,调用 uni.getLocation返回结果传递给组件时,需指定 type 为 gcj02
- type: 'gcj02',
- geocode: true,
- success: function(data) {
- resolve(data)
- },
- fail: function(err) {
- reject(err)
- },
- complete() {
- uni.hideLoading();
- }
- })
- // #endif
- //app结束
-
- ///小程序开始
- // #ifdef MP
- var amapPlugin = new amap.AMapWX({
- key: '9054f24276297df42fd1b6044f91abc5' //此处为高德平台申请的微信小程序的key
- });
- uni.showLoading({
- title: '获取信息中'
- });
- amapPlugin.getRegeo({
- success: function(data) {
- resolve(data)
- },
- fail: function(err) {
- reject(err)
- },
- complete: function() {
- uni.hideLoading();
- }
- });
- // #endif
- //小程序结束
- })
- };
- export default {
- getlocation: getlocation
- }
|