Find nearest annotations from user location ios?

前提是你 提交于 2020-01-02 17:52:31

问题


I have an array of annotations.I am calculating the distance between each annotation from user location.What I want to know is, how can I find lets say 3 nearest annotations from these distances?

Here is how I am calculating distance from user location to annotations.

  CLLocation *userLocation = self.mapView.userLocation.location;

  for (Annotation *obj in annotations) {

       CLLocation *loc = [[CLLocation alloc] initWithLatitude:obj.coordinate.latitude longitude:obj.coordinate.longitude];
      //  CLLocation *userLocation = [[CLLocation alloc] initWithLatitude:self.mapView.userLocation.coordinate.latitude longitude:self.mapView.userLocation.coordinate.longitude];
        CLLocationDistance dist = [loc distanceFromLocation:userLocation];
        int distance = dist;
        NSLog(@"Distance from Annotations - %i", distance);


    }

回答1:


The userLocation is static, so don't recreate it each time in your loop. You can also get the location directly with:

CLLocation *userLocation = self.mapView.userLocation.location;

Then, you could do something like, store your distance numbers and annotations into a dictionary, with the distances as keys and the annotations as the values. Once the dictionary is built, sort the allKeys array and then you can get the annotations associated with the first 3 keys.


Assuming that you always have at least one annotation when you run this:

CLLocation *userLocation = self.mapView.userLocation.location;
NSMutableDictionary *distances = [NSMutableDictionary dictionary];

for (Annotation *obj in annotations) {
    CLLocation *loc = [[CLLocation alloc] initWithLatitude:obj.coordinate.latitude longitude:obj.coordinate.longitude];
    CLLocationDistance distance = [loc distanceFromLocation:userLocation];

    NSLog(@"Distance from Annotations - %f", distance);

    [distances setObject:obj forKey:@( distance )];
}

NSArray *sortedKeys = [[distances allKeys] sortedArrayUsingSelector:@selector(compare:)];
NSArray *closestKeys = [sortedKeys subarrayWithRange:NSMakeRange(0, MIN(3, sortedKeys.count))];

NSArray *closestAnnotations = [distances objectsForKeys:closestKeys notFoundMarker:[NSNull null]];



回答2:


NSArray *numbers = [NSArray arrayWithObjects:
                    [NSNumber numberWithInt:0],
                    [NSNumber numberWithInt:3],
                    [NSNumber numberWithInt:2],
                    [NSNumber numberWithInt:8],
                    nil];

NSSortDescriptor* sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:nil ascending:YES selector:@selector(localizedCompare:)];
NSArray *sortedNumbers = [numbers sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];

you store all distance in an array and sort it and then pick up top there number

[sortedNumbers objectAtIndex:0];
[sortedNumbers objectAtIndex:1];
[sortedNumbers objectAtIndex:2];


来源:https://stackoverflow.com/questions/22452509/find-nearest-annotations-from-user-location-ios

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