2012-06-05 4 views
5

200 개가 넘는 개체가있는 배열이 있는데 각 개체를 통해 루프를 수행하려고합니다.다른 핀 색상의지도보기 주석

각 개체에는 예/아니요 필드가 있으며 그 예/아니요 값에 따라 다른 색의 표식을 표시하고 싶습니다.

내가 볼 수있는 것은 내 루프가 각 객체를 먼저 통과 한 다음 모든 객체의 끝에 모든 주석이 추가된다는 것입니다.

모든 주석을 내 맵에 추가 할 때 yes no 값의 배열을 통해 루프를 검사하므로 패턴이 배치 될 때 어레이의 마지막 객체에서 yes/no 값을 사용합니다 모든.

각 요소의 예/아니요 값에 따라 마커가 달라 지도록하려면 어떻게해야합니까?

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{ 

    MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"]; 

if(current_yesno == YES){ 
    annView.pinColor = MKPinAnnotationColorGreen; 
} 
else 
{ 
    annView.pinColor = MKPinAnnotationColorRed; 
} 
    annView.animatesDrop=NO; 
    annView.canShowCallout = YES; 
    annView.calloutOffset = CGPointMake(-5, 5); 
    return annView; 

} 

current_yesno가 내 .H 파일에 선언 다음과 같이

내 코드 내 주석 코드

for (i = 0; i < [appDelegate.itemArray count]; i++) { 
     item_details *tempObj = [appDelegate.itemArray objectAtIndex:i]; 
     location.latitude = [tempObj.lat floatValue]; 
     location.longitude = [tempObj.lon floatValue]; 
     current_yesno = tempObj.yesno; 
     MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc]initWithTitle:tempObj.name andCoordinate:location]; 
     [self.mapView addAnnotation:newAnnotation]; 
     [newAnnotation release];    
      } 

입니다.

답변

8

addAnnotation을 실행 한 직후에 viewForAnnotation 대리자 메서드를 호출해야 할 필요는 없으며 주석을 보려면 뷰를 가져올 필요가있을 때 맵보기에서 다른 시간에 호출 할 수도 있습니다 (코드가 완전히 다른 작업을 수행하는 동안).

따라서 위임 메소드 외부의 일부 코드와 동기화되는 ivar 값에 의존 할 수 없습니다. 대신

, 주석을 만들 때 그것을 설정, 사용자 정의 MapViewAnnotation 클래스에 yesno 속성을 추가 한 다음 annotation 매개 변수를 통해 viewForAnnotation에 그 값을 액세스 (예.지도보기는 당신에게 정확한 주석 객체에 대한 참조를주고 그 에 대한 전망을 원한다).

예 :

다음
MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc] init... 
newAnnotation.yesno = tempObj.yesno; // <-- set property in annotation 
[self.mapView addAnnotation:newAnnotation]; 

viewForAnnotation의 : 답장을

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation 
{ 
    if (![annotation isKindOfClass:[MapViewAnnotation class]]) 
    { 
     // Return nil (default view) if annotation is 
     // anything but your custom class. 
     return nil; 
    } 

    static NSString *reuseId = @"currentloc"; 

    MKPinAnnotationView *annView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseId]; 
    if (annView == nil) 
    { 
     annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId];   
     annView.animatesDrop = NO; 
     annView.canShowCallout = YES; 
     annView.calloutOffset = CGPointMake(-5, 5); 
    } 
    else 
    { 
     annView.annotation = annotation; 
    } 

    MapViewAnnotation *mvAnn = (MapViewAnnotation *)annotation; 
    if (mvAnn.yesno) 
    { 
     annView.pinColor = MKPinAnnotationColorGreen; 
    } 
    else 
    { 
     annView.pinColor = MKPinAnnotationColorRed; 
    } 

    return annView; 
} 
+0

감사합니다. 어떻게하면 yesno 속성에 MapViewAnnotation을 추가 할 수 있습니까? – user1096447

+1

MapViewAnnotation.h에서 MapViewAnnotation.m에'@property (nonatomic, assign) BOOL yesno;'를 넣고'@synthesize yesno; '를 입력하십시오 – Anna

+0

감사합니다 안나, 관련 답변을 추가하십시오. –

0
MKPinAnnotationView *pin = (MKPinAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier: @"id"]; 
if (pin == nil) 
{ 
    pin = [[MKPinAnnotationView alloc] initWithAnnotation: annotation reuseIdentifier: @"id"] ; 
} 
else 
{ 
    pin.annotation = annotation; 
} 

pin.pinTintColor=[UIColor blueColor]; 
pin.canShowCallout = true; 
관련 문제