简体   繁体   中英

How i can calculate distance between three or four latitude-longitude points? (Haversine formula)

I have haversine formula for two points and its works great. But how i can find distance between three or more lat/lon points?

Example haversine formula for two points:

// C# program for the haversine formula 
using System; 
class GFG 
{ 

static double haversine(double lat1, double lon1, 
                        double lat2, double lon2) 
{ 
    // distance between latitudes and longitudes 
    double dLat = (Math.PI / 180) * (lat2 - lat1); 
    double dLon = (Math.PI / 180) * (lon2 - lon1); 

    // convert to radians 
    lat1 = (Math.PI / 180) * (lat1); 
    lat2 = (Math.PI / 180) * (lat2); 

    // apply formulae 
    double a = Math.Pow(Math.Sin(dLat / 2), 2) +  
               Math.Pow(Math.Sin(dLon / 2), 2) *  
               Math.Cos(lat1) * Math.Cos(lat2); 
    double rad = 6371; 
    double c = 2 * Math.Asin(Math.Sqrt(a)); 
    return rad * c; 
} 

// Driver Code 
public static void Main() 
{ 
    double lat1 = 51.5007; 
    double lon1 = 0.1246; 
    double lat2 = 40.6892; 
    double lon2 = 74.0445; 
    Console.WriteLine(haversine(lat1, lon1,  
                                lat2, lon2) + " K.M."); 
} 
} 

Build a graph where each point is a node and distance between two points(nodes) is the weight of the edge between these two nodes. Then run the Dijkstra's algorithm from the starting point, you will find the distance (shortest paths) between all points from the starting point.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM