geolocation

阅读数:273 发布时间:2016-06-25 17:21:06

作者:gsh 标签: 地理位置信息

简介

Geolocation API用于将用户当前地理位置信息共享给信任的站点,这涉及用户的隐私安全问题,所以当一个站点

需要获取用户的当前地理位置,浏览器会提示用户是“允许” or “拒绝”。

navigator对象中,只包含3个方法:

  • getCurrentPosition
  • watchPosition
  • clearWatch

代码实例

<html>   
<head>   
<meta http-equiv="Content-Type" contentType="text/html; charset=UTF-8" %>   
<title>HTML5 物理定位</title>   
<script type="text/javascript">  
window.onload=function(){  
    var options={  
            enableHighAccuracy:true,  
            maximunAge:1000,  
            timeout:45000  
    };  
    if(window.navigator.geolocation){  
        navigator.geolocation.getCurrentPosition(successCallback,errorCallback,options);  
    }else{  
        alert("你的浏览器不支持定位!");  
    }  
}  
function successCallback(position){  
    var output="";  
    output +="Your position has bean located . 
";  
    output+=" Latitude:"+position.coords.latitude+" ";  
    output+=" Longitude:"+position.coords.longitude+" ";  
    output+=" Accuracy :"+position.coords.accuracy +" meters";  
    if(position.coords.latitude){  
        output+=" Accuracy :"+position.coords.altitudeAccuracy +" meters";  
    }  
    if(position.coords.heading){  
        output+=" Heading :"+position.coords.Heading +" meters";  
    }  
    if(position.coords.speed){  
        output+=" Speed :"+position.coords.Speed +" m/s";  
    }  
    output+=" Time of Position "+position.timestamp +" m/s";  
    alert(output);  
}  
function errorCallback(error){  
   switch(error.code){  
   case error.PERMISSION_DENIED:  
       alert("you have denied access to your position .");  
       break;  
   case error.POSITION_UNAVAILABLE:  
       alert("there was a problem getting yout position .");  
       break;  
   case error.TIMEOUT:  
       alert("The application has timed out attempting to get your location .");  
       break;  

   }  
}  
</script>  
</head>   
<body >   

</body>   
</html>   

相关文章推荐: