简体   繁体   中英

how can find nearest place image using current location in php?

I have a database with a of stores with latitudes and longitudes of current location. So based on the current (lat, lng) location that I input, I would like to get a list of items from those within some radius like 1 km, 5km etc ?

You can get all result( latitude, longitude and distance ) from database record by using MySQL query like below:

SELECT latitude, longitude, SQRT(
    POW(69.1 * (latitude - [startlat]), 2) +
    POW(69.1 * ([startlng] - longitude) * COS(latitude / 57.3), 2)) AS distance
FROM TableName HAVING distance < 5 ORDER BY distance;

Here:
startlat is your desired latitude,
startlng is your desired latitude,
69.1 is the conversion factor for miles to latitude degrees,
57.3 is roughly 180/pi,
5 is the search radius in miles

I filter this result by these records which having < 5 distance .

Here is a mysql code, i used some years ago:

SELECT
`id`,
`name`,
ACOS( SIN( RADIANS( `latitude` ) ) * SIN( RADIANS( $fLat ) ) + COS( RADIANS( `latitude` ) )
* COS( RADIANS( $fLat )) * COS( RADIANS( `longitude` ) - RADIANS( $fLon )) ) * 6380 AS `distance`
FROM `stations`
WHERE
ACOS( SIN( RADIANS( `latitude` ) ) * SIN( RADIANS( $fLat ) ) + COS( RADIANS( `latitude` ) )
* COS( RADIANS( $fLat )) * COS( RADIANS( `longitude` ) - RADIANS( $fLon )) ) * 6380 < 10
ORDER BY `distance`

请阅读以下内容: http : //www.movable-type.co.uk/scripts/latlong-db.html它告诉您如何做。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM