Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,227 questions

56,129 answers

573 users

How to calculate the distance between two latitude-longitude points in C#

1 Answer

0 votes
using System;

class HaversineDistance
{
    // ------------------------------------------------------------
    // Convert degrees to radians
    // ------------------------------------------------------------
    static double DegToRad(double deg)
    {
        return deg * Math.PI / 180.0;
    }

    // ------------------------------------------------------------
    // Compute the great-circle distance between two points on Earth
    // using the Haversine formula.
    // lat1, lon1, lat2, lon2 are in degrees.
    // The result is returned in kilometers.
    // ------------------------------------------------------------
    static double Haversine(double lat1, double lon1,
                            double lat2, double lon2)
    {
        // Earth's mean radius in kilometers
        const double R = 6371.0;

        // Convert all angles to radians
        double rlat1 = DegToRad(lat1);
        double rlon1 = DegToRad(lon1);
        double rlat2 = DegToRad(lat2);
        double rlon2 = DegToRad(lon2);

        // Differences
        double dlat = rlat2 - rlat1;
        double dlon = rlon2 - rlon1;

        // Haversine formula
        // a is the Haversine of the central angle between the two points.
        double a = Math.Sin(dlat / 2) * Math.Sin(dlat / 2) +
                   Math.Cos(rlat1) * Math.Cos(rlat2) *
                   Math.Sin(dlon / 2) * Math.Sin(dlon / 2);

        // c is the central angle between the two points on the Earth’s surface.
        double c = 2 * Math.Asin(Math.Sqrt(a));

        // Final distance
        return R * c;
    }

    static void Main()
    {
        // Example coordinates:
        // Austin, Texas
        double lat1 = 30.2672;
        double lon1 = -97.7431;

        // Houston, Texas
        double lat2 = 29.7604;
        double lon2 = -95.3698;

        double distanceKm = Haversine(lat1, lon1, lat2, lon2);

        // Convert kilometers to miles
        double distanceMiles = distanceKm * 0.621371;

        Console.WriteLine("Distance: " + distanceKm.ToString("0.000") + " km");
        Console.WriteLine("Distance: " + distanceMiles.ToString("0.000") + " miles");
    }
}



/*
run:

Distance: 235.352 km
Distance: 146.241 miles

*/

 



answered Aug 7 by avibootz

Related questions

...