2010-12-30 2 views
0

에 정렬 된 주석의 제한된 수의 표시 나는 500 주석의 NSArray를 가지고 있고 기본적으로 사용자 (나머지는지도보기에 추가되지 않을 것이다)가지도보기

만 열 가까운 주석을 표시하려면 어떻게해야 그렇게? 여기

내 코드입니다 :

-(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

addAnnotations 방법은 배열을 기대 ... 난 내 코드에 이것을 추가했습니다,하지만 여전히 작동하지 않습니다 MKAnnotation 프로토콜을 준수하는 개체 전달중인 배열에는 일반 사전 객체 만 포함되어있는 것처럼 보입니다.

annotations 배열을 통과하는 루프로 addAnnotations 행을 대체하고 각 사전 객체에서 MKPointAnnotation 객체를 생성해야합니다 (좌표를 가져 와서 MKPointAnnotation 객체를 초기화하십시오). 그런 다음 for 루프 내에서 addAnnotation (단수)을 호출하고 MKPointAnnotation 객체를 전달합니다.

+0

이 작업을 10 회 반복 할 수 있습니까? 어떻게 그럴 수 있니? \t CLLocationCoordinate2D poiCoordinate; \t poiCoordinate.latitude = [[중요 값 valueForKey : @ "workingCoordinate.latitude"] doubleValue]; \t poiCoordinate.longitude = [[중요 값 valueForKey : @ "workingCoordinate.longitude"] doubleValue]; \t MyHomeAnnotation * poiAnnotation = [MyHomeAnnotation의 ALLOC] \t \t \t \t \t \t \t \t \t initWithCoordinate : poiCoordinate \t \t \t \t \t \t \t \t \t 제목 : poiDict valueForKey @ "자막"] \t \t \t \t \t \t \t \t \t]; \t [mapView addAnnotation : poiAnnotation]; \t [self adjustMapZoom]; \t [poiAnnotation release]; – danskcollignon

+0

예 : for (YourArray의 NSDictionary * poiDict) {여기에있는 귀하의 의견에있는 코드}'. 루프 내에서 adjustMapZoom을 호출하지 않습니다. for-loop 다음에 한 번 호출해야합니다. – Anna

+0

감사합니다. 새로운 코드를 검토해 주시겠습니까? 여전히 작동하지 않습니다 :( – danskcollignon