版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a0405221/article/details/84929157
为了满足定位需求,先是调了各大地图的ip转经纬度(精确到城市,甚至出现精确到省的情况),而后使用百度地图的Geolocation定位,出于好奇找出了百度Geolocation定位的源码,才发现如此简单,在此跟大家分享。
核心对象
1.Geolocation是window.navigator下面的一个对象,该对象提供了实现地理位置定位的接口。
2.要用该功能首先判断浏览器是否支持navigator.geolocation该对象。
开始使用
navigator.geolocation.getCurrentPosition(function (position) {
console.log('success',position);
var success = {
coords: {
accuracy: 70,// 位置精确度
altitude: null, // 海拔
altitudeAccuracy: null,// 海拔精确度
heading: null,// 朝向
latitude: 23.1017714, // 纬度
longitude: 113.25838209999999,// 经度
speed: null,// 速度
},
timestamp: 1544340493718
}
}, function (error) {
console.log('error',error);
var error = {
code: error.code,
message: error.message,
jsapiLoc: true
};
}, {
enableHighAccuracy: false,
timeout: 5000,
maximumAge: 600000
});
option
options是一个对象,可以设置超时时间、缓存时间等, 如下:
A.enableHighAccuracy 表示是否允许使用高精度,但这个
参数在很多设备上设置了都没用,设备综合考虑电量、
地理情况等,很多时候都是默认的由设备自身来调整。
B.timeout 指定超时时间
C.maximumAge 是指缓存的时间