<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>HTML5地理定位</title>
</head>
<body>
<h4>获取用户位置Demo1</h4>
<p id="demo1">点击按钮获取当前坐标(可能需要比较长的时间)</p>
<button onclick="getLocation1()">获取位置</button>
<script>
var x1 = document.getElementById("demo1");
function getLocation1() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showLocation2);
}else {
x.innerHTML = "该浏览器不支持获取地理位置";
}
}
function showLocation2(pos) {
x.innerHTML = "维度:" + pos.coords.latitude + "<br> 经度:" + pos.coords.longitude;
}
</script>
<br><br><br>
<h4>获取地理位置Demo2</h4>
<p id="demo2">获取地理位置(可能需要较长的时间)</p>
<button id="btn2" onclick="getLocation2()">获取地理位置</button>
<script>
var x2 = document.getElementById("demo2");
function getLocation2() {
if (navigator.geolocation){
navigator.geolocation.getCurrentPosition(showLocation2,showError2)
} else {
x2.innerHTML = "你的浏览器暂不支持获取地理位置";
}
}
function showLocation2(pos) {
x2.innerHTML = "维度:" + pos.coords.latitude + "经度:" + pos.coords.longitude;
}
function showError2(error) {
switch (error.code) {
case error.PERMISSION_DENIED:
x2.innerHTML = "用户拒绝位置请求";
break;
case error.POSITION_UNAVAILABLE:
x2.innerHTML = "位置信息不可用";
break;
case error.TIMEOUT:
x2.innerHTML = "请求用户位置超时";
break;
case error.UNKNOWN_ERR:
x2.innerHTML = "未知错误";
break;
}
}
</script>
<br><br><br>
<h4>在地图上显示结果</h4>
<p id="demo3">点击按钮获取位置</p>
<button id="btn3" onclick="getLocation3()">获取位置</button>
<script>
var x3 = document.getElementById("demo3");
function getLocation3() {
if (navigator.geolocation){
navigator.geolocation.getCurrentPosition(showLocation3,showError3);
} else {
x3.innerHTML = "你的浏览器暂不支持定位";
}
}
function showLocation3(pos) {
var latlon = pos.coords.latitude+","+pos.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 + "'>";
}
function showError3(error) {
switch (error.code) {
case error.PERMISSION_DENIED:
x3.innerHTML = "用户不允许定位";
break;
case error.POSITION_UNAVAILABLE:
x3.innerHTML = "定位信息不准确";
break;
case error.TIMEOUT:
x3.innerHTML = "定位请求超时";
break;
case error.UNKNOWN_ERR:
x3.innerHTML = "未知错误";
}
}
</script>
<br><br><br>
<h4>继续定位</h4>
<p id="demo4">点击按钮定位</p>
<button id="btn4" onclick="getLocation1()">获取位置</button>
<script>
var x4 = document.getElementById("demo4");
function getLocation4() {
if (navigator.geolocation){
navigator.geolocation.watchPosition(showLocation4);
} else {
x4.innerHTML = "你的浏览器暂不支持定位";
}
}
function showLocation4(pos) {
x4.innerHTML = "维度:" + pos.coords.latitude + "经度:" + pos.coords.longitude;
}
</script>
</body>
</html>
1、HTML5 Geolocation(地理定位)
HTML5 Geolocation(地理定位)用于定位用户的位置。
定位用户的位置
HTML5 Geolocation API 用于获得用户的地理位置。
鉴于该特性可能侵犯用户的隐私,除非用户同意,否则用户位置信息是不可用的。
2、检测是否支持地理定位
如果支持,则运行 getCurrentPosition() 方法。如果不支持,则向用户显示一段消息。
如果getCurrentPosition()运行成功,则向参数showPosition中规定的函数返回一个coordinates对象
showPosition() 函数获得并显示经度和纬度
3、getCurrentPosition() 方法 - 返回数据
T若成功,则 getCurrentPosition() 方法返回对象。始终会返回 latitude、longitude 以及 accuracy 属性。如果可用,则会返回其他下面的属性。
属性 描述
coords.latitude 十进制数的纬度
coords.longitude 十进制数的经度
coords.accuracy 位置精度
coords.altitude 海拔,海平面以上以米计
coords.altitudeAccuracy 位置的海拔精度
coords.heading 方向,从正北开始以度计
coords.speed 速度,以米/每秒计
timestamp 响应的日期/时间
4、Geolocation 对象 - 其他有趣的方法
watchPosition() - 返回用户的当前位置,并继续返回用户移动时的更新位置(就像汽车上的 GPS)。
clearWatch() - 停止 watchPosition() 方法
下面的例子展示 watchPosition() 方法。您需要一台精确的 GPS 设备来测试该例(比如 iPhone):
网友评论