Click here to Skip to main content
13,050,450 members (77,325 online)
Click here to Skip to main content
Add your own
alternative version


4 bookmarked
Posted 30 Aug 2012

Latitude and longitude

, 30 Aug 2012
Rate this:
Please Sign up or sign in to vote.
This is an alternative for "Latitude and longitude"


The simple code allows you to measure the distance between two places on earth with latitude and longitude pairs in meters.


Finding proximity between two points is important. In day to day life we speak about distances in Meters and KM. But mathematically how to convert the distance between two {latitude,longitude} pairs?

Using the code 

The logic is simple. Convert the values to radians. Than calculate d=sine(latitude distance)^2+cosine(longitude distance). distance=2* tan2(sqrt(d),sqrt(1-d)). Multiply the distance with earth radius to get geographical distance in radians. All you have to do is get it converted to meters.  

public class DistanceInMeter
public static double distFrom(double lat1, double lng1, double lat2, double lng2) {
    double earthRadius = 3958.75;
    double dLat = toRadians(lat2-lat1);
    double dLng = toRadians(lng2-lng1);
    double a = Math.Sin(dLat/2) * Math.Sin(dLat/2) +
               Math.Cos(Math.toRadians(lat1)) * Math.Cos(Math.toRadians(lat2)) *
               Math.Sin(dLng/2) * Math.Sin(dLng/2);
    double c = 2 * Math.Atan2(Math.sqrt(a), Math.sqrt(1-a));
    double dist = earthRadius * c;

    int meterConversion = 1609;

    return (dist * meterConversion);

	static double  toRadians(double degree)
		return (degree*0.0174532925 );

Points of Interest

You can import Google map in you application and find all places within some distance. This function is typically helpful for spatial data mining. 


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

CEO Integrated Ideas
India India
gasshopper.iics is a group of like minded programmers and learners in codeproject. The basic objective is to keep in touch and be notified while a member contributes an article, to check out with technology and share what we know. We are the "students" of codeproject.

This group is managed by Rupam Das, an active author here. Other Notable members include Ranjan who extends his helping hands to invaluable number of authors in their articles and writes some great articles himself.

Rupam Das is mentor of Grasshopper Network,founder and CEO of Integrated Ideas Consultancy Services, a research consultancy firm in India. He has been part of projects in several technologies including Matlab, C#, Android, OpenCV, Drupal, Omnet++, legacy C, vb, gcc, NS-2, Arduino, Raspberry-PI. Off late he has made peace with the fact that he loves C# more than anything else but is still struck in legacy style of coding.
Rupam loves algorithm and prefers Image processing, Artificial Intelligence and Bio-medical Engineering over other technologies.

He is frustrated with his poor writing and "grammer" skills but happy that coding polishes these frustrations.
Group type: Organisation

116 members

You may also be interested in...

Comments and Discussions

GeneralYou should state your assumptions Pin
John Brett31-Aug-12 0:47
memberJohn Brett31-Aug-12 0:47 
GeneralRe: You should state your assumptions Pin
Grasshopper.iics31-Aug-12 15:01
groupGrasshopper.iics31-Aug-12 15:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 31 Aug 2012
Article Copyright 2012 by Grasshopper.iics
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid