4

I have a website where I store locations in a MySQL database and GeoCode using the address provided by the database.

Now I want to add a feature which will find the closest places to the specified point e.g. the location they are standing now or typing. I'm coding the geocoder and google maps in JavaScript and jQuery and I get the results using PHP to fetch from the database.

I read about the viewport but I can't figure out how it would work in my situation where I need to show my own database locations and not Google's.

Can anybody help? And it would be awesome if you have some kind of example.

6
  • Please give a concrete example. I mean, an actual address you are using. I am confused by your question. Sep 2, 2012 at 10:00
  • If e.g i wanted to find a specific bar in copenhagen i would save the address e.g "Vester voldgade 10 København V", send it to the geocoder and display it in google maps using the lat and longitude i get. Now i want to find places nearby this bar and display them. Sep 2, 2012 at 10:04
  • So Vester voldgade 10 is in your database, and you want to find other bars near Vester Voldgade 10? Sep 2, 2012 at 10:09
  • Yes for example. But it could also be an address specified by the user, and then return all results within 1 kilometer or 500 yards or something. Sep 2, 2012 at 10:12
  • This is a pure SQL question, not Google Maps or PHP.
    – Marcelo
    Sep 2, 2012 at 10:16

3 Answers 3

4

radius search:

select *,
    acos(cos(centerLat * (PI()/180)) *
     cos(centerLon * (PI()/180)) *
     cos(lat * (PI()/180)) *
     cos(lon * (PI()/180))
     +
     cos(centerLat * (PI()/180)) *
     sin(centerLon * (PI()/180)) *
     cos(lat * (PI()/180)) *
     sin(lon * (PI()/180))
     +
     sin(centerLat * (PI()/180)) *
     sin(lat * (PI()/180))
    ) * 3959 as Dist
from TABLE_NAME
having Dist < radius
order by Dist

3959 is the Earth radius in Miles. Replace this value with radius in KM, or any unit, to get results on the same unit. centerLat and centerLon are the center of the search (your input), while lat and lon are fields in the table.

3
  • where do you get centerlat from? Sep 2, 2012 at 10:30
  • Did you read the entire post? (including the note below the code)?
    – Marcelo
    Sep 2, 2012 at 10:32
  • sorry must have skipped that. Sep 2, 2012 at 10:36
2

Here's what I would do:

  • Since you use a database anyway, convert and save the human-readable address to a LatLng (once) every time you add a place to your DB

  • Query user for his/her location

  • geocode the user location to a LatLng

  • use a SQL query to find places matching desired distance. (see here https://developers.google.com/maps/articles/phpsqlsearch_v3)

SELECT id, ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < 25 ORDER BY distance LIMIT 0 , 20;

  • display SQL results back to Javascript map
1
  • i thought about saving my lat/longs, and that is a good idea. Thank you i will try it out. Sep 2, 2012 at 10:32
0

Sounds like the classic store locator. See this article in the Google Maps API v3 documentation for an example using PHP and MySQL.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.