mui框架混合app关于定位问题

来源:互联网 发布:vsftpd 修改端口 编辑:程序博客网 时间:2024/05/29 16:37

第一次做app,经验并不丰富,碰见的第一个难题就是定位的问题用mui来定位的话偏差太大,最后只能选择百度的,通过手机GPS获取经纬度,然后给百度纠正一下,最后拿到的就是准确的坐标;注明(并不是手机gps不准,是因为国内的政策,怕是一些不法分子干一些坏事,所以故意搞得偏差,还好有百度,高德也可以的),但是百度的秘钥每天访问次数是有限制的;所以你做的app如果用户群体大的话,那还是建议花钱买商业版付费的;

上代码:

还有在html里边要写上script 标签来写你的秘钥这里就不多说了



js代码

function ininMap() {
map = new BMap.Map("container"); //创建一个地图实例,HTML容器包含地图  
var point = new BMap.Point(currentX, currentY);
map.centerAndZoom(point, 15);
map.enableScrollWheelZoom();
map.addControl(new BMap.GeolocationControl()); // 定位控件   
var geolocation = new BMap.Geolocation(); //百度包装好的定位库  
geolocation.getCurrentPosition(function(r) {
if(this.getStatus() == BMAP_STATUS_SUCCESS) {
var mk = new BMap.Marker(r.point);
map.addOverlay(mk);
map.panTo(r.point);
currentX = r.point.lng;
currentY = r.point.lat;
console.log('您的位置:' + r.point.lng + ',' + r.point.lat);
var myGeo = new BMap.Geocoder();
// 根据坐标得到地址描述    
myGeo.getLocation(new BMap.Point(currentX, currentY), function(result) {
if(result) {
//                      alert(result.address);
var adress = result.address;
//读取服务器数据
var param = {
"lgtd": currentX,
"lttd": currentY,
"userid": loginUserId,
"identity": Identity,
"position": adress
};
                 
ajax(url, param, function(data) {

console.log("发送成功了")
});


}
console.log(result.address)
});


} else {
alert('failed' + this.getStatus());
}
}, {
enableHighAccuracy: true
});


}

原创粉丝点击