navigator.geolocation例子
- <html>
- <head>
- <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>
- </head>
- <body>
- <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
- <script type="text/javascript">
- if(navigator.geolocation){
- navigator.geolocation.getCurrentPosition(getPositionSuccess, getPositionError);
- }else{
- alert("您的浏览器不支持Geolocation!");
- }
- </script>
- </body>
- </html>
转载:http://blog.csdn.net/gohome520/article/details/7338486