how to find nearest latitude and longitude form cu

2020-05-01 09:30发布

问题:

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