Google Maps - How to get the distance between two point in metre?

后端 未结 6 509

I have these coordinate :

(45.463688, 9.18814) 
(46.0438317, 9.75936230000002)

and I need (trought Google API V3, I think) to get the dista

6条回答
  •  甜味超标
    2020-11-28 23:14

    Try this:

    const const toRadians = (val) => {
        return val * Math.PI / 180;
    }
    const toDegrees = (val) => {
        return val * 180 / Math.PI;
    }
    // Calculate a point winthin a circle
    // circle ={center:LatLong, radius: number} // in metres
    const pointInsideCircle = (point, circle) => {
        let center = circle.center;
        let distance = distanceBetween(point, center);
    
        //alert(distance);
        return distance < circle.radius;
    };
    
    const distanceBetween = (point1, point2) => {
       //debugger;
       var R = 6371e3; // metres
       var φ1 = toRadians(point1.latitude);
       var φ2 = toRadians(point2.latitude);
       var Δφ = toRadians(point2.latitude - point1.latitude);
       var Δλ = toRadians(point2.longitude - point1.longitude);
    
       var a = Math.sin(Δφ / 2) * Math.sin(Δφ / 2) +
               Math.cos(φ1) * Math.cos(φ2) *
               Math.sin(Δλ / 2) * Math.sin(Δλ / 2);
       var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    
       return R * c;
    }
    

    References: http://www.movable-type.co.uk/scripts/latlong.html

提交回复
热议问题