Geospatial coordinates and distance in kilometers

后端 未结 5 1659
礼貌的吻别
礼貌的吻别 2020-12-02 11:31

This is a followup to this question.

I seem to be stuck on this. Basically, I need to be able to convert back and forth to referring to coordinates either in the st

5条回答
  •  孤街浪徒
    2020-12-02 12:11

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

    That site has a lot of different formulas and Javascript code that should help you out. I've successfully translated it into both C# and a SQL Server UDF and I use them all over the place.

    For example for Javascript to calculate distance:

    var R = 6371; // km
    var φ1 = lat1.toRadians();
    var φ2 = lat2.toRadians();
    var Δφ = (lat2-lat1).toRadians();
    var Δλ = (lon2-lon1).toRadians();
    
    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));
    
    var d = R * c; 
    

    Enjoy!

提交回复
热议问题