Geo Search Without Distance Geo Search Without Distance php php

Geo Search Without Distance


10 km in a straight line is: on the longitude is equal to ~6 minutes

The distance to longitude ratio is not a constant - it depends on the the user's latitude. You can use this function for the longitude difference:

function distanceToLng($distance,$latDeg) {    $latRad = $latDeg / 180 * M_PI;    $R = 6371;    return atan2(1*sin($distance/$R)*cos($latRad), cos($distance/$R) - sin($latRad)*sin($latRad)) * (180 / M_PI);}

The formula is taken from this question.

You can calculate latitude difference from a given distance using something like this:

$lngDeg = $distanceInKm/6371 * 180 / M_PI


As the other answer noted you really want to use the Haversine Formula which takes into account the users latitude.

Haversine Formula: http://en.wikipedia.org/wiki/Haversine_formula

This other answer has a ton of great resources to accomplish this in PHP or SQL:

MySQL Great Circle Distance (Haversine formula)