bjorn2404 / jQuery-Store-Locator-Plugin

A store locator plugin using Google Maps API version 3
MIT License
495 stars 235 forks source link

Only show nearest #27

Closed bre7 closed 10 years ago

bre7 commented 10 years ago

Has anyone been able to do this ? Only show the nearest location or none, I want to avoid showing all of them. @bjorn2404 explained it on the blog: http://www.bjornblog.com/web/jquery-store-locator-plugin#comment-15972 but is there a way to do it server side ?

thanks..awesome plugin by the way

bjorn2404 commented 10 years ago

What do you mean by server side?

bre7 commented 10 years ago

Instead of "SELECT * FROM location", I'd like to known if it's possible to send the location and only receive the nearest store (and prevent the client from receiving the entire list).

bjorn2404 commented 10 years ago

If you pass the origin latitude and longitude over you can do the distance formula in the query but if you have lots of locations it's going to be pretty slow. If you don't have a lot be sure to index the latitude and longitude columns and you can do something like the following. Note the $origLat and $origLng variables and the "locations" table twice.

SELECT * FROM locations
JOIN(
SELECT (3956 * 2 * ASIN( LEAST(1, SQRT( ( POW(SIN((RADIANS(`lat`) - RADIANS($origLat)) / 2), 2) + COS(RADIANS($origLat)) * COS(RADIANS(`lat`)) * POW(SIN((RADIANS(`lng`) - RADIANS($origLng)) / 2), 2) ) ) ) ))
AS distance FROM locations
) AS location
WHERE `lat` BETWEEN $origLat - (50 / 69) AND $origLat  + (50 / 69) AND `lng` BETWEEN $origLng - (50 / (69 * COS(RADIANS($origLat)))) AND $origLng + (50 / (69 * COS(RADIANS($origLng))))
ORDER BY distance
LIMIT 1
bre7 commented 10 years ago

Thanks bjorn