简体   繁体   中英

Display a limited number of sorted annotations in Mapview

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM