12

An question on latitude and longitude...Probably a silly one.

I have a set of latitude and longitudes for various locations.And, I have the latitude and longitude of the location in which I am in.

Now,I have to find the nearest places from my locations from the set of latitudes and longitudes I have. Is thee any special way of calculating it or it is just mere subtraction and checking the difference?

Could you please throw some light on this?

Thanks, J

Abhishek
  • 6,862
  • 22
  • 62
  • 79
  • 1
    Might want to do a seach of SO - there are plenty of existing answers to this. e.g. http://stackoverflow.com/questions/27928 and http://stackoverflow.com/questions/23569/calculating-distance-between-2-cities and – Rosstified Feb 01 '10 at 02:13

2 Answers2

13

It depends on how the points are placed.

For example - if most of the points are in a parking lot, then Euclidean Distance should work well.

In other cases - Geodesic Distance needs to be computed. This link should help you with more information.

Here is the conversion from Decimal format to Degree-Minute-Second format and vice versa.

cheers

Arnkrishn
  • 29,828
  • 40
  • 114
  • 128
  • Hi Andriyev, Thanks for the information. All of my latitudes and longitudes are of the form "13.048869". Is there a way I could convert them to "53 09 02N" format? Sorry if its a way too dumb. Thanks,J – Abhishek Feb 01 '10 at 02:23
  • @jadaaih - Your lat, long is in decimal format. Updated the response with a link, which is about the conversion you seek. – Arnkrishn Feb 01 '10 at 02:35
  • I have the similar requirement, I have the list of coordinates for a larger building and I need to find which one of the list is nearest to me. So as per the answer given by @Andriyev Eculidean Distance should work, but it will be really helpful if you (or anyone) throw some more light on same to implement it in code level. I need to implement it in JAVA (Android) – Chimu Jan 29 '14 at 10:54
7

You can use this SQL. it will select then nearest Lat, Lng from your DB entries.

SELECT id,lat,lng, ((ACOS(SIN(your_lat * PI() / 180) * SIN(lat * PI() / 180) + COS(your_lat * PI() / 180) * COS(lat * PI() / 180) * COS((your_long - lng) * PI() / 180)) * 180 / PI()) * 60 * 1.1515) AS distance FROM your_table_name HAVING distance <='10' ORDER BY distance ASC LIMIT 0,10

Hope this will help.

Pritesh Mahajan
  • 4,974
  • 8
  • 39
  • 64
  • The unit is miles. To get the distance in kilometers, you have to multiply the term "[...]60 * 1.1515[...]" with 1.609344 (which is 1 mile in km) which results in: "[...]60 * 1.1515 * 1.609344[...]". – user1337 Mar 02 '18 at 13:22