最近开发过程中,遇到了需求,要求能查询出手机当前的位置,由于不是正式的app形式,只能通过html5的地理定位方法去获取。
navigator.geolocation的三个方法:
1. getCurrentPosition()
2. watchPosition()
3. clearWatch()
getCurrentPosition()
使用方法:navigator.geolocation.getCurrentPosition(successCallback, [errorCallback] , [positionOptions]);
A) successCallback 获取定位成功时执行的回调函数 eg: function(position){alert("纬度:"+position.coords.latitude+";经度:"+position.coords.longitude)};
successCallback返回一个地理数据对象position作为参数,该对象有属性
使用之前要先检查浏览器是否支持:
<script type="text/javascript">
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(getPositionSuccess, getPositionError);
}else{
alert("您的浏览器不支持Geolocation!");
}
</script>
以及调用成功和失败的回调函数
<script type="text/javascript">
function getPositionSuccess(position){
var lat = position.coords.latitude;
var lng = position.coords.longitude;
document.write("所在位置: 经度(" + lat + "),纬度(" + lng + ")");
if(typeof position.address !== "undefined"){
var country = position.address.country;
var province = position.address.region;
var city = position.address.city;
document.write("<br />");
document.write("您位于" + country + province + city);
}
}
function getPositionError(error){
switch(error.code){
case error.TIMEOUT:
alert("连接超时,请重试");
break;
case error.PERMISSION_DENIED:
alert("您拒绝了使用位置共享服务,查询已取消");
break;
case error.POSITION_UNAVAILABLE:
alert("非常抱歉,我们暂时无法为您所在的星球提供位置服务");
break;
}
}
</script>
B) errorCallback 定位失败时执行的回调函数 eg: function(error){alert(error.message);}
errorCallback返回一个错误数据对象error作为参数,该对象有属性:
1.code :表示失败原因,返回1 or 2 or 3 ,具体为
(数值为1) 表示没有权限使用地理定位API
POSITION_UNAVAILABLE (数值为2) 表示无法确定设备的位置,例如一个或多个的用于定位采集程序报告了一个内部错误导致了全部过程的失败
TIMEOUT (数值为3) 表示超时
详情查看 http://dev.w3.org/geo/api/spec-source.html#permission_denied_error
2.message :错误提示内容
C) positionOptions 用来设置positionOptions来更精细的执行定位,positionOptions拥有三个属性{enableHighAccuracy:boolean , timeout:long , maximumAge:long}。
enableHighAccuracy 【true or false(默认)】是否返回更详细更准确的结构,默认为false不启用,选择true则启用,但是会导致较长的响应时间及增加功耗,这种情况更多的用在移动设备上。
timeout 设备位置获取操作的超时时间设定(不包括获取用户权限时间),单位为毫秒,如果在设定的timeout时间内未能获取位置定位,则会执行errorCallback()返回code(3)。如果未设定timeout,那么timeout默认为无穷大,如果timeout为负数,则默认timeout为0。
maximumAge 设定位置缓存时间,以毫秒为单位,如果不设置该值,该值默认为0,如果设定负数,则默认为0。该值为0时,位置定位时会重新获取一个新的位置对象;该值大于0时,即从上一次获取位置时开始,缓存位置对象,如果再次获取位置时间不超过maximumAge,则返回缓存中的位置,如果超出maximumAge,则重新获取一个新的位置。
watchPosition()
功能getCurrentPosition()相似,watchPosition()是定期轮询设备的位置,同样拥有3个参数,与getCurrentPosition()相同。
使用方法:navigator.geolocation.watchPosition(successCallback, [errorCallback] , [positionOptions]);
执行步骤:
1.首先初始化positionOptions内的属性(详细同上)。
2.判断是否有缓存位置对象,该对象年龄是否可用、是否超出maximumAge ,如果可用且未超出,返回缓存位置,否则重新确定设备位置。
3.位置定位操作:
i.建立一个计时器,进行位置获取操作,如果在timeout之前完成,执行下一步;如果未在timeout之前完成,则执行errorCallback(),code为3,跳出步骤做等待重新激活。
ii.如果在timeout之前获得位置成功,则执行successCallback(),然后重置计时器(从获取位置成功时刻重新算起),继续挂起获取新位置。当有与之前位置有明显不同位置出现时,再次执行successCallback(),并重复操作,该循环直到timeout超时或者获取操作中遇到POSITION_UNAVAILABLE错误执行errorCallback()为止,亦或者执行clearWatch()操作。
clearWatch()
配合watchPosition()使用,用于停止watchPosition()轮询。
watchPosition()需要定义一个watchID,var watchID = watchPosition(...),通过clearWatch(watchID)来停止watchPosition(),使用方法类似setInterval。