Hacker News new | ask | show | jobs
by codesink 5708 days ago
true, the distance calculation must NOT be in the WHERE clause if you want to use indexes (and you want).

What I'm doing, given a max distance and a search point, is to calculate the bounding box in which I want to search in filter results with

WHERE lat BETWEEN lat_min AND lat_max AND lng BETWEEN lng_min AND lng_max

Calculating latitude min/max is trivial knowing that 1 latitude degree is 111.2KM. Longitude is a bit more convoluted because longitude degree size changes moving north/south.

$lat_min = $lat - $range_km * (1 / 111.2); $lat_max = $lat + $range_km * (1 / 111.2);

$k = $range_km/6371.04; $lng_min = $lng - rad2deg($k/cos(deg2rad($lat))); $lng_max = $lng + rad2deg($k/cos(deg2rad($lat)));