Calculate distance between two points in google maps V3

后端 未结 15 1705
醉梦人生
醉梦人生 2020-11-22 02:27

How do you calculate the distance between two markers in Google maps V3? (Similar to the distanceFrom function inV2.)

Thanks..

15条回答
  •  广开言路
    2020-11-22 03:07

    Had to do it... The action script way

    //just make sure you pass a number to the function because it would accept you mother in law...
    public var rad = function(x:*) {return x*Math.PI/180;}
    
    protected  function distHaversine(p1:Object, p2:Object):Number {
        var R:int = 6371; // earth's mean radius in km
        var dLat:Number = rad(p2.lat() - p1.lat());
        var dLong:Number = rad(p2.lng() - p1.lng());
    
        var a:Number = Math.sin(dLat/2) * Math.sin(dLat/2) +
                    Math.cos(rad(p1.lat())) * Math.cos(rad(p2.lat())) * Math.sin(dLong/2) * Math.sin(dLong/2);
        var c:Number = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
        var d:Number = R * c;
    
        return d;
    }
    

提交回复
热议问题