H5获取地理位置
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
<title>H5获取地理位置</title>
</head>
<body>
<script type="text/javascript">
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(getLocation, showError);
} else {
alert("浏览器不支持")
}
function getLocation(position) {
lat = position.coords.latitude;
lng = position.coords.longitude;
alert(lat + "," + lng);
}
function showError(error) {
switch(error.code) {
case error.TIMEOUT:
alert("请求超时!请再试一次!");
break;
case error.POSITION_UNAVAILABLE:
alert('我们找不到你的位置 Sorry! ios10必须https才能访问定位');
break;
case error.PERMISSION_DENIED:
alert('请允许地理位置访问!');
break;
case error.UNKNOWN_ERROR:
alert('发生了未知错误!');
break;
}
}
</script>
</body>
</html>