一:H5定位(应用到app中有定位不准的情况)
getLocation();//html5定位
function getLocation(){
if (navigator.geolocation){
navigator.geolocation.getCurrentPosition(geo_success,geo_error); //成功方法,失败方法
}else{
alert("不支持地理定位。");
}
}
function geo_success(position) {
//获取经纬度
var polygon = position.coords.longitude+","+position.coords.latitude;
//.....
}
function geo_error(error) {
switch(error.code) {
case error.PERMISSION_DENIED:
mui.toast("获取商圈信息失败,请在设置中开启定位权限");
break;
case error.POSITION_UNAVAILABLE:
mui.toast("获取商圈信息失败,位置信息是不可用");
break;
case error.TIMEOUT:
mui.toast("获取商圈信息失败,请求获取用户位置超时");
break;
case error.UNKNOWN_ERROR:
mui.toast("获取商圈信息失败,定位系统失效");
break;
}
}
二:GPS定位(大部分手机不用打开gps定位功能即可操作),使用到了H5+的方法
plus.geolocation.getCurrentPosition(getGPSInfo, function(e) {
if (e.code == 2) { //未开启GPS,则调用百度定位(有偏差)
var geolocation = new BMap.Geolocation();
geolocation.getCurrentPosition(function(r){
getLocation(r);
},{enableHighAccuracy: true});
}else{
mui.alert("为了给您提供更好的服务,请进入(设置→隐私→定位服务),打开服务","提示");
}
});
//GPS定位
function getGPSInfo(position){
var codns = position.coords; //获取地理坐标信息;
var xx = codns.longitude; //获取到当前位置的经度
var yy = codns.latitude; //获取到当前位置的纬度;
}