admin管理员组文章数量:1356319
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=";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.sphericalputeDistanceBetween(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>
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./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.puteDistanceBetween(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>
Share
Improve this question
asked Apr 19, 2013 at 18:19
user2275870user2275870
4
- Is latLngA a position? Are it's coordinates set? – Jensd Commented Apr 19, 2013 at 18:23
- Yes, it's the current position. – user2275870 Commented Apr 19, 2013 at 18:24
-
its
NaN
because yourlatLngA = NaN
– David Chase Commented Apr 19, 2013 at 18:39 -
The LatLng Constructor Documentation
LatLng(lat:number, lng:number, noWrap?:boolean)
– david strachan Commented Apr 19, 2013 at 18:39
1 Answer
Reset to default 7Simplified 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.puteDistanceBetween(latLngA, latLngB);
alert(distance);//In metres
},
function() {
alert("geolocation not supported!!");
}
);
}
本文标签:
版权声明:本文标题:javascript - Calculate distance between current location and and a different location using Google Maps API V3 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743969773a2570544.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论