Display a limited number of sorted annotations in Mapview

孤街浪徒 提交于 2019-12-12 06:12:41

问题


I have an NSArray of 500 annotations and I basically want to show only the ten nearest annotations to the user (the rest would not be added on Mapview)

How should I do that?

Here is my code:

-(void) loadAndSortPOIs {
 [poiArray release];
 nextPoiIndex = 0;
 NSString *poiPath = [[NSBundle mainBundle] pathForResource:@"annotations"
              ofType:@"plist"];
 poiArray = [[NSMutableArray alloc] initWithContentsOfFile:poiPath];
 CLLocation *homeLocation = [[CLLocation alloc] 
        initWithLatitude:homeCoordinate.latitude
        longitude:homeCoordinate.longitude];
 for (int i = 0; i < [poiArray count]; i++) {
  NSDictionary *dict = (NSDictionary*) [poiArray objectAtIndex: i];
  CLLocationDegrees storeLatitude = [[dict objectForKey:@"workingCoordinate.latitude"] doubleValue];
  CLLocationDegrees storeLongitude = [[dict objectForKey:@"workingCoordinate.longitude"] doubleValue];
  CLLocation *storeLocation = [[CLLocation alloc]
          initWithLatitude:storeLatitude
          longitude:storeLongitude];
  CLLocationDistance distanceFromHome = [storeLocation distanceFromLocation: homeLocation];
  [storeLocation release];


  NSMutableDictionary *mutableDict = [[NSMutableDictionary alloc]
           initWithDictionary:dict];
  [mutableDict setValue:[NSNumber numberWithDouble:distanceFromHome]
        forKey:@"distanceFromHome"];
  [poiArray replaceObjectAtIndex:i withObject:mutableDict];
  [mutableDict release];

 }


// now sort by distanceFromHome

 NSArray *sortDescriptors = [NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:@"distanceFromHome" ascending:YES] autorelease]];

 [poiArray sortUsingDescriptors:sortDescriptors];
 NSLog (@"poiArray: %@", poiArray);

 [homeLocation release];

EDIT: I've added this to my code, but it still doesn't work...

- (void) displayPois {
    [mapView removeAnnotations: mapView.annotations];
for(int i = 0; i < 10; i++) {
    NSDictionary *poiDict = [poiArray objectAtIndex:nextPoiIndex++];
    CLLocationCoordinate2D poiCoordinate;
    poiCoordinate.latitude = [[poiDict valueForKey:@"workingCoordinate.latitude"] doubleValue];
    poiCoordinate.longitude = [[poiDict valueForKey:@"workingCoordinate.longitude"] doubleValue];
    MyHomeAnnotation *poiAnnotation = [[MyHomeAnnotation alloc]
                                       initWithCoordinate:poiCoordinate
                                       title:[poiDict valueForKey:@"Subtitle"]
                                       ];
        [mapView addAnnotation:poiAnnotation];
        [self loadAndSortPOIs];
    [poiAnnotation release];

    }   [self adjustMapZoom];
}

回答1:


The addAnnotations method expects an array of objects that conform to the MKAnnotation protocol. The array you are passing it seems to just contain plain dictionary objects.

You'll need to replace the addAnnotations line with a loop that goes through your annotations array and creates an MKPointAnnotation object from each dictionary object (pull out the coordinates and init the MKPointAnnotation object with them). Then (inside the for-loop) call addAnnotation (singular) and pass it the MKPointAnnotation object.



来源:https://stackoverflow.com/questions/4564335/display-a-limited-number-of-sorted-annotations-in-mapview

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