开发移动端时,总是用到定位功能,但苹果是不支持第三方定位功能,只能针对手机自身定位功能进行封装!
代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>调用手机原生定位功能</title>
</head>
<body>
<button οnclick="getGeocode()">获取定位</button>
<div id='text'></div>
</body>
<script>
function geoInf(position) {
var str = "";
str += "地址:" + position.addresses + "\n";//获取地址信息
str += "这是定位地市:" + position.address.city + "\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));
document.getElementById("text").innerHTML = str;
}
// 通过定位模块获取位置信息
function getGeocode() {
plus.geolocation.getCurrentPosition(geoInf, function (e) {
alert("获取定位位置信息失败:" + e.message);
}, { geocode: true });
}
</script>
</html>