如何从当前位置find最近的经纬度?
我有一个收集了n个经纬度和地址的字典。 我想计算在ios中哪个(纬度和lang度)点离当前用户位置(纬度,lang度)最近?
提前致谢
如果你想根据坐标计算,这里是简单的场景:只要从你的字典中获取所有的"CLLocationCoordinate2D"
。
例如:
MKPointAnnotation *annotaion=[MKPointAnnotaion alloc]alloc]; CLLocationCoordinate2D pointACoordinate = [annotation coordinate]; // If you have the co-ordinates in hand , then just make " CLLocation *loc2; " using Co-ordinates loc2 = [[CLLocation alloc] initWithLatitude:pointACoordinate.latitude longitude:pointACoordinate.longitude];
然后把循环或无论你想要计算更less的距离与所有使用这个比较
CLLocationDistance nearLocation=[ location distanceFromLocation:loc2]; // location --> your current Location
如果你想达到比它更好的saury answer
:那么只需要遵循saury answer
,他们很好的达到完美。
这个问题类似于findK最近邻(KNN)问题。 我会build议你通过下面的链接了解如何解决你的问题
- KNN Wiki
- 有详细的概述的好博客