0

OK so I don't have an issue here but I'm just wondering if there's a more standardized way to handle what I'm doing.

Essentially I have a DB table full of locations including longitude and Latitude, there could potentially be thousands of locations. I also have some functionality to search your postcode and you can then see from the stored the locations the closest x amount to you.

Ive read about going off and using the Google Maps api to do this but I don't really want to pull back and send thousands of requests to the google maps api.

So here's what I'm doing. I have a stored procedure where I am passing the users Long and Lat. I am then using this to form a column called distance with which I am then ordering the data. The distance column I am working out using the below logic:

SQRT(SQUARE((CAST(USERSLAT AS decimal(9,6))) - Latitude) + SQUARE((CAST(USERSLONG AS decimal(9,6)))-(Longitude))) AS Distance

Essentially what this is doing is the classic a^2=b^2+c^2 to find the distance between to coords, and then using these results I can theoretically see the closest locations to the user. Once I have this data i can use the google maps api to find the exact distances. Is this an ok way to do things? I have this nagging feeling in the back of my head that im missing something.

maniak1982
  • 707
  • 2
  • 7
  • 23
Srb1313711
  • 2,017
  • 5
  • 24
  • 35
  • 2
    Despite the fact that simpe pythagoras won´t work for a sphere? You may read this before: https://en.wikipedia.org/wiki/Great-circle_distance – MakePeaceGreatAgain Jun 23 '15 at 11:26
  • 1
    checkout this answer http://stackoverflow.com/a/15063233/1370442 since sql server 2008 you can do it all natively in tsql ;o) – Luke Baughan Jun 23 '15 at 11:28
  • @HimBromBree yes thank you, just realized that Pythag wont work for this:-) But i mean I can avoid using the gmaps api? Theres a formula somewhere I can use? – Srb1313711 Jun 23 '15 at 11:37
  • @bUKaneer Thanks this interesting – Srb1313711 Jun 23 '15 at 11:40
  • 3
    You want the Haversine formula - see one implementation [here](http://stackoverflow.com/questions/28542133/i-cannot-get-the-same-accuracy-as-google-maps-when-it-comes-to-distance/28543001#28543001). – sideroxylon Jun 23 '15 at 12:45
  • Thanks for the responses, I'm going with @bUKaneer recommendation – Srb1313711 Jun 23 '15 at 13:31

0 Answers0