how to find nearest latitude and longitude form current place?

倖福魔咒の 提交于 2019-12-20 06:06:04

问题


I have a dictionary with a collection of n number of latitude, longitude and address. I want to calculate which (latitude and langitude)point is closest from current user location(latitude ,langitude) in ios?

Thanks in advance


回答1:


If you want to calculate based on the co-ordinates , here is simple scenario : Just take all of the "CLLocationCoordinate2D" from your dictionary .

For ex:

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];

then put loop or whichever you want to calculate the less distance by comparing with all using this

CLLocationDistance nearLocation=[ location distanceFromLocation:loc2]; // location --> your current Location

If you want to achieve other than it : Then just follow saury answer, they are good to achieve perfectly.




回答2:


This problem is similar to finding the K Nearest Neighbor (KNN) problem. I will suggest you to go through following links to have an understanding on how to solve your problem

  1. KNN Wiki
  2. Good blog having detailed overview


来源:https://stackoverflow.com/questions/17085668/how-to-find-nearest-latitude-and-longitude-form-current-place

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!