$("#btn").click(function() {
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition, showError);
} else {
console.log("Geolocation is not supported by this breoser")
}
});
var xhtml = '';
/*function showPositon(position) {
xhtml = 'x:' + position.coords.latitude + 'y:' + position.coords.longitude;
$("#input").html(xhtml);
}*/
/*Google地图展示当前位置*/
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";
xhtml = "<img src='" + img_url + "' />";
$("#input").html(xhtml);
}
function showError(error) {
switch(error.code) {
case error.PERMISSION_DENIED:
xhtml = "User denied the request for Geolocation."
$("#input").html(xhtml);
break;
case error.POSITION_UNAVAILABLE:
xhtml = "Location information is unavailable."
$("#input").html(xhtml);
break;
case error.TIMEOUT:
xhtml = "The request to get user location timed out."
$("#input").html(xhtml);
break;
case error.UNKNOWN_ERROR:
xhtml = "An unknown error occurred."
$("#input").html(xhtml);
break;
}
}
Geolocation对象----其他有趣的方法
watchPosition()---返回用户当前位置,并继续返回用户移动时的更新位置(就像汽车上的GPS)
clearPosition()---停止watchPosition()方法
H5地理定位
最新推荐文章于 2024-08-08 23:45:45 发布