如何使用HTML5地理位置经纬度API?
HTML5地理位置API允许您与您喜欢的网站共享您的位置。JavaScript可以捕获您的纬度和经度,并可以发送到后端网络服务器,并且可以执行高级位置感知操作,例如查找当地商家或在地图上显示您的位置。
地理位置API可以使用全局navigator对象的新属性,即地理位置对象。

示例
您可以尝试运行以下代码,以使用地理位置API查找当前位置,其中包括纬度和经度坐标
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
function showLocation(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
alert("Latitude : " + latitude + " Longitude: " + longitude);
}
function errorHandler(err) {
if(err.code == 1) {
alert("Error: Access is denied!");
} else if( err.code == 2) {
alert("Error: Position is unavailable!");
}
}
function getLocation(){
if(navigator.geolocation){
// timeout at 60000 milliseconds (60 seconds)
var options = {timeout:60000};
navigator.geolocation.getCurrentPosition
(showLocation, errorHandler, options);
} else{
alert("Sorry, browser does not support geolocation!");
}
}
</script>
</head>
<body>
<form>
<input type="button" onclick="getLocation();" value="Get Location"/>
</form>
</body>
</html>
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C编程
C++
C#
MongoDB
MySQL
Javascript
PHP