Hbuider hybrid app开发之地图位置信息操作方法

/**
 * 位置工具
 * */
var watchId = null;
/**
 * @description 监听位置变化信息
 * */ 
function watchPosition(){
    if (watchId) {
        return;
    }
    watchId = plus.geolocation.watchPosition(function(p){
        geoInf(p);
        postToServer(p);
    }, function(e){
        alert("Geolocation error: " + e.message);
    }); 
}

/**
 * @description 通过定位模块获取位置信息
 * */ 
function getGeocode() {
    plus.geolocation.getCurrentPosition(geoInf, function(e) {
        mui.alert("获取定位位置信息失败:" + e.message);
    }, {
        geocode: true,
        provider: 'amap'
    });
}
/**
 * @description 上传位置信息
 * */ 
function postToServer(position) {
    var str = "";
    str += "地址:" + position.addresses + "\n"; //获取地址信息
    str += "坐标类型:" + position.coordsType + "\n";
    var timeflag = position.timestamp; //获取到地理位置信息的时间戳;一个毫秒数;
    str += "时间戳:" + timeflag + "\n";
    var codns = position.coords; //获取地理坐标信息;
    var lat = codns.latitude; //获取到当前位置的纬度;
    str += "纬度:" + lat + "\n";
    var longt = codns.longitude; //获取到当前位置的经度
    str += "经度:" + longt + "\n";
    var alt = codns.altitude; //获取到当前位置的海拔信息;
    str += "海拔:" + alt + "\n";
    var accu = codns.accuracy; //地理坐标信息精确度信息;
    str += "精确度:" + accu + "\n";
    var altAcc = codns.altitudeAccuracy; //获取海拔信息的精确度;
    str += "海拔精确度:" + altAcc + "\n";
    var head = codns.heading; //获取设备的移动方向;
    str += "移动方向:" + head + "\n";
    var sped = codns.speed; //获取设备的移动速度;
    str += "移动速度:" + sped;
    console.log(JSON.stringify(position));
}
/**
 * @description 处理位置信息
 * */ 
function geoInf(position) {
    var str = "";
    str += "地址:" + position.addresses + "\n"; //获取地址信息
    str += "坐标类型:" + position.coordsType + "\n";
    var timeflag = position.timestamp; //获取到地理位置信息的时间戳;一个毫秒数;
    str += "时间戳:" + timeflag + "\n";
    var codns = position.coords; //获取地理坐标信息;
    var lat = codns.latitude; //获取到当前位置的纬度;
    str += "纬度:" + lat + "\n";
    var longt = codns.longitude; //获取到当前位置的经度
    str += "经度:" + longt + "\n";
    var alt = codns.altitude; //获取到当前位置的海拔信息;
    str += "海拔:" + alt + "\n";
    var accu = codns.accuracy; //地理坐标信息精确度信息;
    str += "精确度:" + accu + "\n";
    var altAcc = codns.altitudeAccuracy; //获取海拔信息的精确度;
    str += "海拔精确度:" + altAcc + "\n";
    var head = codns.heading; //获取设备的移动方向;
    str += "移动方向:" + head + "\n";
    var sped = codns.speed; //获取设备的移动速度;
    str += "移动速度:" + sped;
    console.log(JSON.stringify(position));
}
/**
 * @description 停止监听位置变化信息
 * */ 
function clearWatch(){
    if (watchId) {
        plus.geolocation.clearWatch(watchId);
        watchId = null;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值