HTML5 地理定位
HTML5 Geolocation。
位置
HTML5 Geolocation API 置。
。
Internet Explorer 9、Firefox、Chrome、Safari 以及 Opera 位。
注释:对于拥有 GPS 如 iPhone
HTML5 - 位
请使用 getCurrentPosition() 。
和纬度。
实例
<script>
var x=document.getElementById("demo");
function getLocation()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition);
}
else{x.innerHTML="Geolocation is not supported by this browser.";}
}
function showPosition(position)
{
x.innerHTML="Latitude: " + position.coords.latitude +
"<br />Longitude: " + position.coords.longitude;
}
</script>
- 则运行 getCurrentPosition()
- 如果getCurrentPosition()则向参数showPositioncoordinates对象
- showPosition() 纬度
理。
拒绝
getCurrentPosition() 的函数:
实例
function showError(error)
{
switch(error.code)
{
case error.PERMISSION_DENIED:
x.innerHTML="User denied the request for Geolocation."
break;
case error.POSITION_UNAVAILABLE:
x.innerHTML="Location information is unavailable."
break;
case error.TIMEOUT:
x.innerHTML="The request to get user location timed out."
break;
case error.UNKNOWN_ERROR:
x.innerHTML="An unknown error occurred."
break;
}
}
- Permission denied - 地理定位
- Position unavailable - 前位置
- Timeout - 操作超时
示结果
实例
function showPosition(position)
{
var latlon=position.coords.latitude+","+position.coords.longitude;
var img_url="http://maps.googleapis.com/maps/api/staticmap?center="
+latlon+"&zoom=14&size=400x300&sensor=false";
document.getElementById("mapholder").innerHTML="<img src='"+img_url+"' />";
}
。
图。
信息
用处。
案例:
- 息
- 导航系统 (GPS)
getCurrentPosition() 方法 - 返回数据
getCurrentPosition() 返回 latitude、longitude 以及 accuracy
| 属性 | 描述 |
|---|---|
| coords.latitude | 纬度 |
| coords.longitude | 经度 |
| coords.accuracy | 位置精度 |
| coords.altitude | 计 |
| coords.altitudeAccuracy | 精度 |
| coords.heading | 计 |
| coords.speed | /每秒计 |
| timestamp | /时间 |
Geolocation 对象 - 方法
watchPosition() - 上的 GPS)。
clearWatch() - 停止 watchPosition() 方法
展示 watchPosition() 的 GPS iPhone):
实例
<script>
var x=document.getElementById("demo");
function getLocation()
{
if (navigator.geolocation)
{
navigator.geolocation.watchPosition(showPosition);
}
else{x.innerHTML="Geolocation is not supported by this browser.";}
}
function showPosition(position)
{
x.innerHTML="Latitude: " + position.coords.latitude +
"<br />Longitude: " + position.coords.longitude;
}
</script>