I am writing a Windows Phone 7 application that should be aware of the location. In particular, I want some kind of code (C #) to start when the phone enters the (fixed) range in a certain place, say, 0.5 miles. I have all the lat / long data for physical locations in memory. I will use the Geo Coordinate Watcher class to get the current coordinates of devices. Now the only trick is to calculate whether the user is within any of the locations.
Thank!
Update : as promised here is a small C # function that uses the Spherical Law of Cosines method to calculate distances. Hope this helps someone else. Note. I am writing a Windows Phone 7 application, so I used the GeoLocation class. If you use "normal" C #, you can change the function to accept the two pairs of coordinates that the function needs.
internal const double EarthsRadiusInKilometers = 6371;
private static double SpericalLawOfCosines(GeoCoordinate from, GeoCoordinate to)
{
return ( Math.Acos (
Math.Sin(from.Latitude) * Math.Sin(to.Latitude) +
Math.Cos(from.Latitude) * Math.Cos(to.Latitude) *
Math.Cos(to.Longitude - from.Longitude)
) * EarthsRadiusInKilometers)
.ToRadians();
}
public static double ToRadians(this double d)
{
return (Math.PI / 180) * d;
}
source
share