pc端定位&移动端定位
1. html5得浏览器定位(http不支持,只能在安全域名https下获取)
getLocation() {
var options = {
enableHighAccuracy: true,
maximumAge: 1000,
};
alert("获取位置信息开始--------->");
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
this.onSuccess,
this.onError,
options
);
} else {
alert("您的浏览器不支持地理位置定位!");
}
}
onSuccess(position) {
console.log(position);
}
onError(error) {
console.log(error);
}
2.通过高德地图获取(ip定位),会有一定误差
let AMap = window.AMap;
AMap.service("AMap.Geolocation", () => {
let geolocation = new AMap.Geolocation({
enableHighAccuracy: true,
timeout: 10000,
zoomToAccuracy: true,
buttonPosition: "LB",
});
geolocation.getCurrentPosition();
AMap.event.addListener(geolocation, "complete", this.onSuccess);
AMap.event.addListener(geolocation, "error", this.onError);
});
补充移动端通过高德地图获取(GPS),定位准确