Calculate distance between 2 GPS coordinates

前端 未结 29 3506
青春惊慌失措
青春惊慌失措 2020-11-21 23:34

How do I calculate distance between two GPS coordinates (using latitude and longitude)?

29条回答
  •  温柔的废话
    2020-11-21 23:52

    If you're using .NET don't reivent the wheel. See System.Device.Location. Credit to fnx in the comments in another answer.

    using System.Device.Location;
    
    double lat1 = 45.421527862548828D;
    double long1 = -75.697189331054688D;
    double lat2 = 53.64135D;
    double long2 = -113.59273D;
    
    GeoCoordinate geo1 = new GeoCoordinate(lat1, long1);
    GeoCoordinate geo2 = new GeoCoordinate(lat2, long2);
    
    double distance = geo1.GetDistanceTo(geo2);
    

提交回复
热议问题