我有一个500个注释的NSArray,我基本上只想向用户显示十个最近的注释(其余的不会添加到Mapview上)
我该怎么做?
这是我的代码:
-(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];
编辑:我已将此添加到我的代码中,但它仍然无效......
- (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];
}
答案 0 :(得分:0)
addAnnotations
方法需要符合MKAnnotation
协议的对象数组。您传递的数组似乎只包含普通的字典对象。
您需要使用遍历注释数组的循环替换addAnnotations行,并从每个字典对象创建一个MKPointAnnotation
对象(拉出坐标并使用它们初始化MKPointAnnotation对象)。然后(在for循环中)调用addAnnotation
(单数)并将其传递给MKPointAnnotation对象。