使用Google Maps API V3计算当前位置和不同位置之间的距离 [英] Calculate distance between current location and and a different location using Google Maps API V3
本文介绍了使用Google Maps API V3计算当前位置和不同位置之间的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<!DOCTYPE html>
< html>
< head>
< script src =http://maps.google.com/maps/api/js?sensor=false&v=3&libraries=geometry>< / script>
< / head>
< body>
< p id =demo>< / p>
< button onclick =getLocation()id =demo>获取当前位置< / button>
< button onclick =console.log(distance)>从当前位置获取ditance到其他位置< / button>
< script>
var lat;
var longt;
var latLngA = new google.maps.LatLng(lat,longt);
var latLngB = new google.maps.LatLng(40.778721618334295,-73.96648406982422);
var distance = google.maps.geometry.spherical.computeDistanceBetween(latLngA,latLngB);
var x = document.getElementById(demo);
函数getLocation(){
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(showPosition);
}
else {
x.innerHTML =此浏览器不支持地理定位。;}
}
function showPosition(position){
lat = position.coords.latitude;
longt = position.coords.longitude;
x.innerHTML =Latitude:+ lat +< br> Longitude:+ longt;
}
< / script>
< / body>
< / html>
解决方案
使用图书馆=几何
function getLocation( ){
navigator.geolocation.getCurrentPosition(
function(position){
var latLngA = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
var latLngB = new google.maps.LatLng(40.778721618334295,-73.96648406982422);
var distance = google.maps.geometry.spherical.computeDistanceBetween(latLngA,latLngB);
alert(distance); / /以米计
},
函数(){
alert(geolocation not supported !!);
}
);
}
I'm trying to calculate the distance between my location and a different location, but all I get is NaN. I'm pretty sure I placed the code in the wrong place in the script. Can somebody help me figuring this issue?
<!DOCTYPE html>
<html>
<head>
<script src="http://maps.google.com/maps/api/js?sensor=false&v=3&libraries=geometry"></script>
</head>
<body>
<p id="demo"></p>
<button onclick="getLocation()" id="demo">get current location</button>
<button onclick="console.log(distance)">get ditance from current location to other location</button>
<script>
var lat;
var longt;
var latLngA = new google.maps.LatLng(lat, longt);
var latLngB = new google.maps.LatLng(40.778721618334295, -73.96648406982422);
var distance = google.maps.geometry.spherical.computeDistanceBetween(latLngA, latLngB);
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){
lat = position.coords.latitude;
longt = position.coords.longitude;
x.innerHTML="Latitude: " + lat + "<br>Longitude: " + longt;
}
</script>
</body>
</html>
解决方案
Simplified script using libraries=geometry
function getLocation() {
navigator.geolocation.getCurrentPosition(
function(position) {
var latLngA = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
var latLngB = new google.maps.LatLng(40.778721618334295, -73.96648406982422);
var distance = google.maps.geometry.spherical.computeDistanceBetween(latLngA, latLngB);
alert(distance);//In metres
},
function() {
alert("geolocation not supported!!");
}
);
}
这篇关于使用Google Maps API V3计算当前位置和不同位置之间的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文