H5+ 跨平台APP - geolocation获取设备当前位置

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/QQ_Empire/article/details/100543755

 1、获取当前设备位置信息getCurrentPosition

plus.geolocation.getCurrentPosition(successCB, errorCB, option);

参数:

plus.geolocation.getCurrentPosition(
    function(p){
        // p为获取成功的定位数据
        alert('纬度:' + p.coords.latitude + '\n经度:' + p.coords.longitude + '\n海拔:' + p.coords.altitude);
        console.log('纬度:' + p.coords.latitude + '\n经度:' + p.coords.longitude + '\n海拔:' + p.coords.altitude)
    }, function(err){
        //失败回调
        console.log('Gelocation Error: code - ' + err.code + '; message - ' + err.message);
});

 以下是获取p对象的位置参数

{
    "coordsType":"wgs84",
    "address":{
        "city":"上海市",
        "country":"中国",
        "district":"浦东新区",
        "street":"祥科路"
    },
    "addresses":"祥科路",
    "coords":{
        "latitude":31.58257147124377, //维度
        "longitude":121.6025356153175, //经度
        "accuracy":65, //精度
        "altitude":7.323573589324951,//海拔
        "heading":null,
        "speed":null,
        "altitudeAccuracy":10 //海拔精度
    },
    "timestamp":1567761997327.038
}

2、监听设备位置变化信息watchPosition

plus.geolocation.watchPosition(successCB, errorCB, option);

 参数:

plus.geolocation.getCurrentPosition(
    function(p){
        // p为获取成功的定位数据
        alert('纬度:' + p.coords.latitude + '\n经度:' + p.coords.longitude + '\n海拔:' + p.coords.altitude);
        console.log('纬度:' + p.coords.latitude + '\n经度:' + p.coords.longitude + '\n海拔:' + p.coords.altitude)
    }, function(err){
        //失败回调
        console.log('Gelocation Error: code - ' + err.code + '; message - ' + err.message);
});

3、关闭监听设备位置信息clearWatch 

plus.geolocation.clearWatch(watchId);

 参数:

  • watchId: ( Number ) 必选 
    需要取消的位置监听器标识,调用watchPosition方法的返回值。
var wid = null;
function onPlusReady(){
	wid = plus.geolocation.watchPosition(function(p){
		alert('Geolocation\nLatitude:' + p.coords.latitude + '\nLongitude:' + p.coords.longitude + '\nAltitude:' + p.coords.altitude);
	}, function(e){
		alert('Geolocation error: ' + e.message);
	});
}
function cancel(){
	plus.geolocation.clearWatch(wid);
	wid = null;
}

猜你喜欢

转载自blog.csdn.net/QQ_Empire/article/details/100543755
今日推荐