-2

In my ASP.NET MVC 5 application I have a ApiController (WebApi) with a method that returns a list of establishments (stored in my database) ordered by Name. It works fine.

Now I want to be able to sort this list by distance. So, the mobile application that consumes the web service would send the user location as parameter and at server side I need to calculate the distance between the user location and the location of each establishment.

What tools can I use to do this? Google Maps API? Another one? I've searched but didn't find anything useful... If anyone knows where to find an example on how to start with this it would be very helpful.

Update

Here are some more details:

  • My database is in SQL Server 2014;
  • Each establishment already has latitude and longitude stored in my database;

I would prefer to get the distance of the shortest path by car...

amp
  • 9,710
  • 15
  • 67
  • 118
  • 1
    Does your establishments stored in database have a geolocation? (latitute, longitude)? If so this might help you: http://stackoverflow.com/questions/6366408/calculating-distance-between-two-latitude-and-longitude-geocoordinates – jpgrassi Dec 25 '15 at 12:50

1 Answers1

1

Assuming you store the geo-location of each establishment in your database (latitude + longitude), it's pretty trivial to calculate the distance between 2 points using the Harvesine formula (and here's some online demo of it in action). And once you know the distance you will be able to sort your results. You haven't provided any details about the database you are using but some datastores already provide this calculation out-of-the-box so that you don't need to be making those calculations on the client.

If on the other hand you do not store the coordinates of your establishments in your datastore, then the first step would of course be to get them from some online service. There are plenty of them, just pick the one that suits you best. Obviously in this case the performance of this calculation would depend on the speed of a third party service.

Darin Dimitrov
  • 960,118
  • 257
  • 3,196
  • 2,876
  • What column type are you using for storing the geo-location? Are you using the special `GEOGRAPHY` column type or just 2 columns with latitude and longitude? As far as shortest distance by car is concerned, I am afraid that in this case you might need to use some third party service that has much more information about roads than just the geo-location of the points. You may checkout the available API that Google Maps provide. – Darin Dimitrov Dec 25 '15 at 15:40
  • I just have two columns with latitude and longitude. You will check Google Maps API. – amp Dec 25 '15 at 17:52