navigator.geolocation的三个方法:
-
getCurrentPosition()
-
watchPosition()
-
clearWatch()
经度 : position.longitude
纬度 : position.latitude
准确度 : position.accuracy
海拔 : position.altitude
海拔准确度 : position.altitudeAcuracy
行进方向 : position.heading
地面速度 : position.speed
请求的时间: new Date(position.timestamp)
navigator.geolocation.getCurrentPosition(function(position){
alert('latitude'+position.coords.latitude)
},function(){
alert('error')
},{
enableHighAccuracy: true, //是否获取高精度经纬度,默认值为false
timeout: 5000, //获取位置信息的超时时间。单位为毫秒(ms),默认值为不超时
maximumAge: 0, //获取位置信息的缓存时间。单位为毫秒(ms),默认值为0(立即更新获取)。如果设备缓存的位置信息超过指定的缓存时间,将重新更新位置信息后再返回。
provider: /*(String 类型 )优先使用的定位模块。可取以下供应者: "system":表示系统定位模块,支持wgs84坐标系; "baidu":表示百度定位模块,支持gcj02/bd09/bd09ll坐标系;
"amap":表示高德定位模板,支持gcj02坐标系。 默认值按以下优先顺序获取(amap>baidu>system),若指定的provider不存在或无效则返回错误回调。
注意:百度/高德定位模块需要配置百度/高德地图相关参数才能正常使用。平台支持Android - 2.2+ (支持),iOS - 4.5+ (支持): provider为“baidu”时,
仅支持bd09ll坐标系,暂不支持高德定位模块。*/
coordsType: /*(String 类型 )指定获取的定位数据坐标系类型。可取以下坐标系类型: “wgs84”:表示WGS-84坐标系;
“gcj02”:表示国测局经纬度坐标系; “bd09”:表示百度墨卡托坐标系; “bd09ll”:表示百度经纬度坐标系;
provider为“system”时,默认使用“wgs84”类型;provider为“baidu”是,默认使用“bd09ll”类型。
如果设置的坐标系类型provider不支持,则返回错误。*/
geocode: /*(Boolean 类型 )是否解析地址信息。解析的地址信息保存到Position对象的address、addresses属性中,
true表示解析地址信息,false表示不解析地址信息,返回的Position对象的address、addresses属性值为undefined,默认值为true。
如果解析地址信息失败则返回的Position对象的address、addresses属性值为null。*/
});
watchPosition()
功能getCurrentPosition()相似,watchPosition()是定期轮询设备的位置,同样拥有3个参数,与getCurrentPosition()相同。
function geo_success(position) {
do_something(position.coords.latitude, position.coords.longitude);
}
function geo_error() {
alert("Sorry, no position available.");
}
var geo_options = {
enableHighAccuracy: true,
maximumAge : 30000,
timeout : 27000
};
var wpid = navigator.geolocation.watchPosition(geo_success, geo_error, geo_options);
使用方法:navigator.geolocation.watchPosition(successCallback, [errorCallback] , [positionOptions]);
clearWatch()
navigator.geolocation.clearWatch(watchID);
配合watchPosition()使用,用于停止watchPosition()轮询。
watchPosition()需要定义一个watchID,var watchID = watchPosition(…),通过clearWatch(watchID)来停止watchPosition(),使用方法类似setInterval。
! 注意: 因为谷歌 在中国的限制 可以使用 IE 或者 国产浏览器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<button onclick="fn()">点击</button>
<script>
function fn(){
navigator.geolocation.getCurrentPosition(function (position) {
console.log(position,'====')
alert('latitude' + position.coords.latitude)
}, function (err) {
// alert('error')
console.log(err)
});
}
</script>
</body>
</html>
参考:https://www.cnblogs.com/junxcode/p/5681215.html
参考:https://www.cnblogs.com/MrZouJian/p/9897930.html