简体   繁体   中英

Algorithm to calculate nearest location based on longitude & latitude

I am currently trying to develop an algorithm to calculate which known locations are closest to current known location.

I have a list of say 100 known locations (meaning I have both long- and latitude). Out of these 100 I pick one location, and after picking that location I want a list to show say the 8 closest known locations to this.

How would a possible solution be to this?

Edit

I am not looking for how to calculate distances. I am looking for how to calculate which known locations lies closest to current location.

Example:

Say the list contains 100 locations of movie theaters. I am at theater 5, and I want to find out what other movie theaters in the list lies nearby. Not the distance, but their location.

There is a Distance Matrix API . This API allows you to calculate distances between some given positions.

You can do this also by your own with a haversine formula

尝试使用最近邻居搜索实现kd树算法。

1st idea: If your "100 known locations" remain mostly the same, you could partition the known locations to smaller groups and maintain the structure. Then just play with the closest group.

More mathematical approaches here

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