Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare lat/lng against another lat/lng [duplicate]

Tags:

php

mysql

I have venues in the database that I store with their latitude and longitude.

I also have the logged in user's latitude and longitude.

I'm developing a function that will allow them to find venues within any given measurement, say 20 miles.

Is it possible to find the outermost lat and long of say 20 miles from the user's lat and long and then somehow write a query to find venues within the circle of distance in MySQL?

Thanks.

like image 725
sark9012 Avatar asked Jan 24 '26 03:01

sark9012


2 Answers

I found a working PHP function:

function getDistanceBetweenPoints($latitude1, $longitude1, $latitude2, $longitude2, $unit = 'Mi') {

    $theta = $longitude1 - $longitude2;
    $distance = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2))) + (cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * cos(deg2rad($theta)));
    $distance = acos($distance);
    $distance = rad2deg($distance);
    $distance = $distance * 60 * 1.1515; switch($unit) {
        case 'Mi': break; case 'Km' : $distance = $distance * 1.609344;
    }
    return (round($distance,2));

}  

Thought this may help others.

like image 154
sark9012 Avatar answered Jan 26 '26 16:01

sark9012


I use this (ported to Objective-C from javascript) - lots of great location equations expressed in javascript http://www.movable-type.co.uk/scripts/latlong.html

like image 42
codebrane Avatar answered Jan 26 '26 16:01

codebrane