Arthur Wang's Blog
Follow me on
  • My General Blog
  • Software Development
    • Latest Articles on Software Development
    • Complete Resources for Developers >
      • Tutorial Information for Developers
      • .NET Developer Blogs
      • Developer Journals and Magazines
      • Developer's Tools
      • Database Development
      • ​Developer Conference and Events
  • Tech
    • Latest Articles on Technology Development
  • Health
  • Money
  • Services
    • Modern Website Design
    • Web Maintenance of Existing Websites Service
    • Corporate Business Consulting Service
  • About
  • Contact
  • Art
  • 中文部落格

How to Calculate Distance between addresses Latitude/Longitude Geo Coordinates

4/22/2014

1 Comment

 
Picture
The EASY way is to have .NET 4 Framework or above and reference System.Device.dll, and using namespace found in System.Device.Location. (You should see it if you are developing a Windows Phone program) There is a method called GetDistanceTo().

Here is a quick example:

var location1 = new GeoCoordinates(-29.83245, 31.04034);
var location2 = new GeoCoordinates(-51.39792, -0.12084);
double distance = location1 .GetDistanceTo(location2);

Please note that the method returns in km (kilometer).

Under the hood
The formulas used in the method is actually using the Haversine method. It assumes that the earth is a perfect sphere rather than an ellipsoid, so as a result, it has an error of less than 0.1 percent.  
Since it didn't account for altitude in its calculation, the distance is shorter than the driving distance.

Here is the LONG way:

    public class GeoCoordinate
    {
        public double Latitude { get; set; }
        public double Longitude { get; set; }
    }

    public class GeoCoordinateTool
    {
        public double Distance(GeoCoordinate loc1, GeoCoordinate loc2, int type)
        {
            //1- miles, other km
            //Use 3960 if you want miles; use 6371 if you want km
            double R = (type == 1) ? 3960 : 6371;          // R is earth radius.
            double dLat = this.toRadian(loc2.Latitude - loc1.Latitude);
            double dLon = this.toRadian(loc2.Longitude - loc1.Longitude);

            double a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Cos(this.toRadian(loc1.Latitude)) * Math.Cos(this.toRadian(loc2.Latitude)) * Math.Sin(dLon / 2) * Math.Sin(dLon / 2);

            double c = 2 * Math.Asin(Math.Min(1, Math.Sqrt(a)));
            double d = R * c;

            return d;
        }

        private double toRadian(double val)
        {
            return (Math.PI / 180) * val;
        }
    }

How to use this class?

            GeoCoordinate g1 = new GeoCoordinate();
            GeoCoordinate g2 = new GeoCoordinate();

            g1.Latitude =-29.83245;
            g1.Longitude = 31.04034;

            g2.Latitude = -51.39792;
            g2.Longitude = -0.12084;

      var geotool = new GeoCoordinateTool();
      var distance = geotool.Distance(g1, g2, 1);
Here are technical references for the formulas:
  • The Haversine Formula (Assume Earth is spherical)
  • The Vincenty's Formula (More Accurate;Using Theoretical Ellipsoid for Earth)
search texts: easiest way to calculate a distance, example of using GetDistanceTo, without Google Maps API
Read Latest Software Development Articles  Read Latest Tech Articles

Picture
Let's Connect!

Arthur Wang

​Arthur is a software developer at his core and has been developing web applications and leading development projects since 2000.  He enjoys learning new technologies and writing about them.


If you would like to read more articles written by Arthur, please subscribe the weekly newsletter from Uniting Digital since he is also a content contributor for the site.
Subscribe

1 Comment
Dating Apps Newcastle link
11/13/2024 01:38:01 pm

Thank you foor sharing this

Reply



Leave a Reply.

    Arthur Wang

    @ArthurWangLA
    MCSD App Builder
    MCSD Web Applications
    ​Member of Windows Insider Program & HoloLens Developer Community & Dev Center Insider Program

    Over 17+ years of  experience in web-based software development & management.  Specialized in Microsoft technology with c# language and its architecture design.  MCSD, MCSE, Microsoft Specialist, MCP + Internet, and B.S. from UCLA

    Archives

    August 2018
    March 2018
    January 2017
    December 2016
    May 2016
    April 2016
    March 2016
    February 2016
    April 2014

    Categories

    All
    API
    Arduino
    ASP.NET
    Cognitive
    CSS
    Database
    Deep Learning
    DevOps
    Electronics
    Flexbox
    HTML5
    IoT
    Katana
    Machine Learning
    Management
    .NET
    .NET Core
    Neural Network
    OWIN
    Programming
    Programming Tools
    Recognition
    Security
    SQL Server
    UWP
    Visual Studio
    Web API
    Web Developer

    RSS Feed

    Latest Articles

© 2014-2020 ArthurWiz.com All Rights reserved. | Home | About |
Protected by Copyscape