Home >Database >Mysql Tutorial >How to Retrieve Data Points Within Overlapping Marker Radii from a Geospatial Database?
Retrieve Results Within Overlapping Marker Radii from Database
The objective is to retrieve data points that fall within the overlapping radii of multiple markers in a geospatial database. To achieve this, you must employ the equation of a circle to determine whether a point lies within a given radius.
Considering User B's location as the circle's center and User A's location as points on the circle's perimeter, the equation of the circle, converted to kilometers for consistency, transforms into:
((x-x1)*111.12)^2 + ((y-y1)*111.12)^2 = 4 (=2^2)
where:
Translating this equation into SQL yields:
SELECT A.user_id, A.radius_id, A.latitude, A.logitude FROM UserA AS A, (SELECT user_id, latitude, longitude FROM UserB WHERE user_id = 8) AS B WHERE (POW((A.latitude-B.latitude)*111.12, 2) + POW((A.longitude - B.longitude)*111.12, 2)) <= 4
Note: For enhanced accuracy, consider using (A.longitude - B.longitude)*111.12*cos(A.latitude) or (A.longitude - B.longitude)*111.12*cos(B.latitude) instead of (A.longitude - B.longitude)*111.12 in the equation.
The above is the detailed content of How to Retrieve Data Points Within Overlapping Marker Radii from a Geospatial Database?. For more information, please follow other related articles on the PHP Chinese website!