最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Calculate distance between current location and and a different location using Google Maps API V3 - Stack Overflow

programmeradmin1浏览0评论

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 your latLngA = 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
Add a ment  | 

1 Answer 1

Reset to default 7

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.puteDistanceBetween(latLngA, latLngB);
                alert(distance);//In metres
            },
            function() {
                alert("geolocation not supported!!");
            }
    );
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论