HTML5 Geolocation用来定位用户的位置。

HTML5 Geolocation用来定位用户的位置。

定位用户的位置

HTMl5 Geolocation API用来得到用户的地理位置。

因为这个可能和个人隐私相关,除非用户允许否则不能使用。

浏览器支持

iefirefoxchromeoperasafari

IE9,Firefox,Chrome,Opera和Safari 5都支持这个特性。

注意:如果使用带有GPS的设备,例如iphone,Geolocation将会更加准确。

HTML5 - 使用Geolocation

使用getCurrentPosition()方法得到用户的位置。

下面这个例子是一个简单的返回用户所在地点经纬度的例子:

 
 
  1. <script>
  2. var x=document.getElementById("demo");
  3. function getLocation()
  4. {
  5. if (navigator.geolocation)
  6. {
  7. navigator.geolocation.getCurrentPosition(showPosition);
  8. }
  9. else{x.innerHTML="Geolocation is not supported by this browser.";}
  10. }
  11. function showPosition(position)
  12. {
  13. x.innerHTML="Latitude: " + position.coords.latitude +
  14. "<br />Longitude: " + position.coords.longitude;
  15. }
  16. </script>

在线演示

代码说明:

  • 首先检查Geolocation是否支持
  • 如果支持,运行getCurrentPosition()方法,如果不支持,显示给用户信息
  • 如果getCurrentPosition()方法成功了,返回一个坐标的对象到参数指定的方法(showPosition)中
  • showPosition()方法得到显示的经纬度

上面的例子是一个基本的Geolocation脚本,没有做错误处理。

处理错误和拒绝

getCurrentPosition()方法的第二个参数用来处理错误。指定一个方法当无法得到用户位置的时候来处理错误。

 
 
  1. function showError(error)
  2. {
  3. switch(error.code)
  4. {
  5. case error.PERMISSION_DENIED:
  6. x.innerHTML="User denied the request for Geolocation."
  7. break;
  8. case error.POSITION_UNAVAILABLE:
  9. x.innerHTML="Location information is unavailable."
  10. break;
  11. case error.TIMEOUT:
  12. x.innerHTML="The request to get user location timed out."
  13. break;
  14. case error.UNKNOWN_ERROR:
  15. x.innerHTML="An unknown error occurred."
  16. break;
  17. }
  18. }

在线演示

错误代码如下:

  • 权限拒绝:用户不需要执行Geolocation
  • 位置不存在:无法得到用户位置
  • 超时:操作超时

在Map中显示结果

为了在map中显示结果,你需要访问一个可以处理经纬度的地图服务,例如,GoogleMaps:

 
 
  1. function showPosition(position)
  2. {
  3. var latlon=position.coords.latitude+","+position.coords.longitude;
  4.  
  5. var img_url="http://maps.googleapis.com/maps/api/staticmap?center="
  6. +latlon+"&zoom=14&size=400x300&sensor=false";
  7.  
  8. document.getElementById("mapholder").innerHTML="<img src=""+img_url+""+img_url+"" />";
  9. }

在上面的例子中,我们使用返回的经纬度来展示位置到Google地图中。如何使用脚本来展示一个互动的地图,带有标记,缩放和拖动功能。

在线演示

地理位置指定信息

这里我们演示了如何展示用户位置到地图上。然而,Geolocation也同样对于地理指定的信息非常有用。

例如:

  • 最新的本地信息
  • 展示用户周围的有趣地方
  • 导航(GPS)

getCurrentPosition()方法 - 返回数据

如果成功getCurrentPosition()方法将返回一个对象。经纬度和准确的信息也会返回。其它属性如果存在也返回。

Property                                                 Description

coords.latitude                                     The latitude as a decimal number

coords.longitude                                 The longitude as a decimal number

coords.accuracy                                  The accuracy of position

coords.altitude                                     The altitude in meters above the mean sea level

coords.altitudeAccuracy                     The altitude accuracy of position

coords.heading                                   The heading as degrees clockwise from North

coords.speed                                      The speed in meters per second

timestamp                                            The date/time of the response

Geolocation 对象 - 其它方法

watchPosition()方法 - 返回目前的用户位置并且当用户移动后继续返回更新的位置(例如车里的GPS)。

clearWatch()方法 - 停止watchPosition()方法

下面的代码展示了如何使用watchPositin()方法,注意你需要一个准确的GPS设备来测试(例如,iphone)

 
 
  1. <script>
  2. var x=document.getElementById("demo");
  3. function getLocation()
  4. {
  5. if (navigator.geolocation)
  6. {
  7. navigator.geolocation.watchPosition(showPosition);
  8. }
  9. else{x.innerHTML="Geolocation is not supported by this browser.";}
  10. }
  11.  
  12. function showPosition(position)
  13. {
  14. x.innerHTML="Latitude: " + position.coords.latitude +
  15. "<br />Longitude: " + position.coords.longitude;
  16. }
  17. </script>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值