我正在开发一个基于移动的网站,在那里我集成了谷歌地图,我需要填写谷歌地图的‘从’字段动态。
有没有可能从web浏览器获取GPS位置,并将其动态填充到Google地图的“from”字段中?
发布于 2010-04-06 04:43:14
如果使用Geolocation API,则只需使用以下代码即可。
navigator.geolocation.getCurrentPosition(function(location) {
console.log(location.coords.latitude);
console.log(location.coords.longitude);
console.log(location.coords.accuracy);
});
您也可以使用Google's Client Location API。
这个问题已经在Is it possible to detect a mobile browser's GPS location?和Get position data from mobile browser中讨论过了。你可以在那里找到更多信息。
发布于 2012-08-24 05:58:54
给出一个更具体的答案。HTML5允许您获取地理坐标,并且它做了一项相当不错的工作。总体而言,浏览器对地理定位的支持相当不错,除了ie7和ie8 (以及opera mini)之外的所有主流浏览器。IE9可以完成这项工作,但它的表现最差。Checkout caniuse.com:
http://caniuse.com/#search=geol
此外,你需要你的用户的批准才能访问他们的位置,所以确保你检查了这一点,并给出了一些像样的指示,以防它被关闭。特别是对于Iphone来说,打开Safari的权限有点麻烦。
发布于 2016-11-25 17:49:55
使用此链接,您将在http://www.w3schools.com/html/html5_geolocation.asp上找到所有信息
<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>
https://stackoverflow.com/questions/2577305
复制相似问题