如题,这是我之前做的小程序中获取位置信息部分,用户只能进行“拒绝”或“允许”获取位置信息操作,“拒绝”则获取位置失败,“允许”则获取用户所在位置信息(该操作只在后台执行,不打开地图查看位置,不能选择、移动、改变位置,这也是我当时没用小程序提供的API的原因,我要的就是用户的位置,不能随意改变)
index.wxml
-
<view class="container">
-
-
<view class="resultPart">
-
-
<text>当前位置:</text>
-
-
<text class="resTxt">{{result}}</text>
-
-
</view>
-
-
</view>
index.wxss
-
.resultPart{
-
-
width:80%;
-
-
display:flex;
-
-
flex-direction:column;
-
-
align-items:center;
-
-
}
-
-
-
-
.resTxt{
-
-
padding-top:40rpx;
-
-
color:blue;
-
-
}
index.js
-
var amapFile = require('../../utils/amap-wx.js');
-
-
Page({
-
-
data: {
-
-
result:''
-
-
},
-
-
onLoad: function () {
-
-
console.log('onLoad');
-
-
this.getLocation();
-
-
},
-
-
getLocation:function(){
-
-
var _this = this;
-
-
var myAmapFun = new amapFile.AMapWX({key:'xxxxxxxx'});//key注册高德地图开发者
-
-
myAmapFun.getRegeo({
-
-
success: function(data){
-
-
console.log('getLocation success');
-
-
_this.setData({
-
-
result:data[0].name+' '+data[0].desc
-
-
});
-
-
},
-
-
fail: function(info){
-
-
console.log("getLocation fail");
-
-
wx.showModal({title:info.errMsg});
-
-
_this.setData({
-
-
result:'获取位置失败!'
-
-
});
-
-
}
-
-
});
-
-
}
-
-
})
-
|