How to search through a list of coordinates and display, depending on the distance between it and current location? (Windows Phone 7)
I have gathered about 60 or so geo coordinates of cinema locations around Ireland and I want to use them in my WP7 app to show nearest cinemas to your current location.
I thought I would save them in an XML file but I don't think that will work for what I'm trying to achieve, which is to be able to search the entire list of coordinates and display whichever cinemas appear within (for example) 25kms of their location.
I have found a few links to helping find the distance between two points so that part shouldn't be too bad.
Any ideas on how I might go about achieving this? Thanks in advance
I created a similar service.
First of all, as Shedal also pointed out, consider hosting the locations in an external service. This will make your life a lot easier when you need to add more locations. (The update application service in App Hub can take up to 10 days which is a long time if you need to quickly fix a wrong location)
Secondly, once you have loaded the list of locations, it is simple to loop through them and find the nearest cinemas. You can use the Haversine formula to calculate the distance between two geo locations (this formula takes into account that the earth is round, and not flat - unlike the Pythagorean theorem). Luckily for your the WP GeoLocation class comes with a built in function called GetDistanceTo that you can use.
The problem is now nothing but a simple Linq query:
List<GeoCoordinate> cinemaLocations = new List<GeoCoordinate>(); GeoCoordinate myLocation = new GeoCoordinate(); var closestCinemas = cinemaLocations.OrderBy(s => s.GetDistanceTo(myLocation)); var closestCinema = closestCinemas.FirstOrDefault();