How to transform a distance from degrees to metres?

前端 未结 3 1416
醉酒成梦
醉酒成梦 2021-02-04 06:31

I\'m using OpenLayers with an ordinary mercator map and I\'m trying to sample a bounding box by finding a grid of points in latlong. The bbox is expressed in latlon, e.g.

<
3条回答
  •  星月不相逢
    2021-02-04 07:14

    Use the haversine formula to get the distance between two points of lat/long. This assumes the earth is a sphere (which is, for most cases, "good enough").

    A Javascript implementation of it (shamelessly stolen from here) looks like this:

    var R = 6371; // km
    var dLat = (lat2-lat1).toRad();
    var dLon = (lon2-lon1).toRad(); 
    var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
            Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) * 
            Math.sin(dLon/2) * Math.sin(dLon/2); 
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
    var d = R * c;
    

提交回复
热议问题