uniapp app端使用h5+ api监听位置变化

学习 · 2022-05-26

实现代码

// 位置变化监听
// 文档地址 https://www.html5plus.org/doc/zh_cn/geolocation.html#
plus.geolocation.watchPosition( position => {
    //console.log('plus.geolocation.watchPosition position', position);
    // todo 缓存定位或者上传定位
    uni.setStorageSync('watchPosition', position)
    // 108.307424,22.799393
    var point = new plus.maps.Point(position.coords.longitude, position.coords.latitude);
    ts.autoUploadPoint({lng:position.coords.longitude,lat:position.coords.latitude})
    plus.maps.Map.reverseGeocode(point, {
        coordType: 'gcj02'
    }, function(event) {
            //console.error('转换后信息', event)
            uni.setStorageSync('reverseGeocode', event)
        },
        function(e) {
            console.error('e',e)
        }
    );
},  
error => {
    console.log('plus.geolocation.watchPosition error', error);
    uni.showModal({  
        title: '提示',  
        content: '请先打开APP位置权限',  
        showCancel: false,  
        success: function (res) {
            if (res.confirm) {
                // 授权
                toAppSettings()
            } 
        }  
    });
},  
{
    // https://www.html5plus.org/doc/zh_cn/geolocation.html#plus.geolocation.PositionOptions
    provider: 'amap',  
    //是否使用高精度设备,如GPS。默认是true  
    enableHighAccuracy: true,  
    //超时时间,单位毫秒,默认为0  
    //使用设置时间内的缓存数据,单位毫秒  
    //默认为0,即始终请求新数据  
    //如设为Infinity,则始终使用缓存数据  
    maximumAge: 30e3,
    // 指定获取的定位数据坐标系类型
    // "wgs84":表示WGS-84坐标系;
    // "gcj02":表示国测局经纬度坐标系;
    // "bd09":表示百度墨卡托坐标系;
    // "bd09ll":表示百度经纬度坐标系;
    // provider为"system"时,支持wgs84坐标系,默认使用"wgs84"坐标系
    // provider为"baidu"时,支持gcj02/bd09/bd09ll坐标系,默认使用"gcj02"坐标系
    // provider为"amap"时,支持gcj02坐标系,默认使用"gcj02"坐标系
    // coordsType: 'wgs84',
    // 是否解析地址信息
    geocode: true
}
uniapp 监听 位置变化 h5+
Theme Jasmine by Kent Liao | 桂ICP备15008025号-6