Geo fencing in PHP and MySQL using latitude, longitude and radius

I have a MySQL table with column latitudes, longitude, userId and radius. When the user provides their current location (lat, long) along with the radius, I want to query the table to provide it with overlapping locations based on two radii.

For example, if the user gives me a latitude of 12.5; longitude 73.5 and a radius of 5 miles; I should be able to retrieve all the records in a MySQL table in which both radii overlap.

My initial thought was to create a bounding box for each lat long in the database (based on radius), and then query location data based on that bounding box. Is this approach right? Is there anything I should worry about if I go this route? Any help that would help me in the right direction would be greatly appreciated.

PS: the link below is what I use as a link.

http://janmatuschek.de/LatitudeLongitudeBoundingCoordinates

+4
source share
2 answers

something like this should do the trick:

SELECT 
    *
FROM
    YOUR_TABLE
WHERE
    SQRT((input_lat - db_lat) * (input_lat - db_lat) + (input_long - db_long) * (input_long - db_long)) <= input_radius

I used this: The distance between two points

: ,

( )

. , 2D 3D.

2D . 1 (x1, y1) 2 (x2, y2).

xd = x2-x1
yd = y2-y1
Distance = SquareRoot(xd*xd + yd*yd)

3D . 1 (x1, y1, z1) 2 (x2, y2, z2).

xd = x2-x1
yd = y2-y1
zd = z2-z1
Distance = SquareRoot(xd*xd + yd*yd + zd*zd)

, , . , , . , .

: , , .

, , , , , , , .

:    SquareRoot (xdxd + ydyd) <

:    (xdxd + ydyd) < ( * )

+3

, , 5 , . , .

mysql gis, , : ( ) , .

, , .

+1

All Articles