简体   繁体   中英

How to get distance to places from DB given longitude and latitude

I want to select the places from the DB in 10 km range around the place where I am. For example, when select Location_1 in my sample DB, I want to get Locations like the above condition from DB. I use Yii2 now.

This is my DB table sample named 'places'

ID          Location        Longitude   Latitude
----------- ---------- ---------- -----------
1           Location_1      30.8505081  14.2575487
2           Location_2      30.828072   14.1554811
3           Location_3      30.850763   14.250705
4           Location_4      30.8484719  14.252653

Now, I have no idea how to get from the Longitude and Latitude to the distance from my current location.

Try this. The location's latitude is 14.2575487 and longitude is 30.8505081 from where you are finding the results from the table within 10 Kms.

SELECT *,ROUND(  1.609 * 3956 * 2  * ASIN(SQRT(POWER(SIN((14.2575487 - ABS(Latitude)) * PI()/180 / 2),2) + COS(14.2575487 * PI()/180 ) * COS(ABS(Latitude) * PI()/180) * POWER(SIN((30.8505081 - Longitude ) * PI()/180 / 2), 2) )),0) AS distance FROM places WHERE 1=1  HAVING distance<= 10 

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