We help IT Professionals succeed at work.

Compare latitude and longitude to see if its within same location - give or take 10 meters

High Priority
347 Views
Last Modified: 2018-07-22
I have the following c# code(all variables are doubles)

 if (Latitude == lastLocation.Latitude && Longitude == lastLocation.Longitude)
                    {

Open in new window


This just compares exact numbers, what i need to do is Compare latitude and longitude to see if in 10 meters of previous location
This code runs 100 times a second, so i need it to be super fast too

I'm not sure how to achieve this so would appreciate some code help
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019
Commented:
You can use the GeoCoordinate() class from System.Device.Location:

var location1 = new GeoCoordinate(lastLocation.Latitude, lastLocation.Longitude); 
var location2 = new GeoCoordinate(Latitude, Longitude);

if ( location1.GetDistanceTo(location2) <= 10 ) {
    Console.WriteLine("Within 10 meters!");
};

Open in new window

Dr. KlahnPrincipal Software Engineer
CERTIFIED EXPERT
Commented:
This is one of those problems that looks superficially simple, but is not when you get down into it.

1 second at the equator is not the same distance as 1 second at the pole, so a fair amount of spherical trigonometry is involved.  Even with optimized code and hardware floating point, running this 100 times a second is a significant load.

But aside from that, if the input data is coming from a GPS receiver the values are not accurate to within 10 meters over the short term.  Averaged over 8 hours, yes, they should be accurate, but not from a defined survey point to any random fix.
websssCEO

Author

Commented:
Thanks Guys

Its for reverse geo coding, I dont want to hit the reverse geo coding servers and user 1 credit if the devices last location is similar to current location

Klahn, good point about spherical projection, doesn't the GeoCoordinate object have this built in?
CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019

Commented:
Calculating distances using LongLat is indeed complicated if you were trying to roll you own solution.. But there's no need to do that because it's already been done for you.

Just use the GeoCoordinate class and don't worry about the maths. The code I posted will run a million time a second if needed.
websssCEO

Author

Commented:
comment  by:Chris Stanyon worked!