Reputation: 31
I am developing a small Windows Phone application. I am storing the current location of user in the database.
Here is the code to retrieve the current location which is stored in database.
public void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
CurrentLatitude = e.Position.Location.Latitude.ToString();
CurrentLongitude = e.Position.Location.Longitude.ToString();
}
We need to allow user to do some activity if he is under the 400 meters from his saved location.
I am using the following code to calculate the distance.
internal double GetDistanceTo(GeoCoordinate ClientLocation)
{
double distanceInMeter;
GeoCoordinate currentLocation = new GeoCoordinate(Convert.ToDouble(watcher.Position.Location.Latitude.ToString()), Convert.ToDouble(watcher.Position.Location.Longitude.ToString()));
distanceInMeter = currentLocation.GetDistanceTo(ClientLocation);
return distanceInMeter;
}
ClientLocation : is the location saved in the database.
So my problem is that it gives extremly large distance even the user is standing at the same location(under 1 meter) which is saved in the database.
Example cordinates (extracted from device)
Saved in Database
Lat : 29.8752546310425
Long: 73.8865985870361
Current Cordinates
Lat : 29.8734102249146
Long :73.9049253463745Distance 1780.45
Could anybody suggest me what is wrong here or a better way to get the distance between two coordinates?
Upvotes: 0
Views: 1534
Reputation: 2216
According to boulter.com it is 1.11miles from A to B. Thats about 1780m, so I dont see what the problem is. Have a read of the Moveable Type help on lat and long calculations.
Upvotes: 3