2010-06-10 7 views
3

나는 질문의 위치/종류에 따라 다른 핀 색상을 나타내는지도를 설정하려고합니다. 이것은 꽤 일반적인 일이지만, viewForAnnotation 델리게이트가 핀 색상을 일관되게 업데이트/설정하도록하는 데 어려움을 겪고 있습니다.
는 내가 설정 기본적으로 사이클 AddressAnnotations의 목록을 다음은 주석 클래스 (버스 정류장, 병원 등)에 따라 showThisLocation 기능이iPhone MapKit 문제 : viewForAnnotation이 일관되게 pinColor를 설정하지 않습니까?


if(myClass == 1){ 
[defaults setObject:@"1" forKey:@"currPinColor"]; 
[defaults synchronize]; 
NSLog(@"Should be %@!", [defaults objectForKey:@"currPinColor"]); 
} else if(myClass ==2){ 
[defaults setObject:@"2" forKey:@"currPinColor"]; 
[defaults synchronize]; 
NSLog(@"Should be %@!", [defaults objectForKey:@"currPinColor"]); 
} 
[_mapView addAnnotation:myCurrentAnnotation]; 

다음 내 viewForAnnotation 위양이,

이처럼 보이는

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { 
    if(annotation == mapView.userLocation){ return nil; } 

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    MKPinAnnotationView *annView = nil; 
    annView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"currentloc"]; 
    if(annView == nil){ 
     annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"]; 
    } 

    annView.pinColor = [defaults integerForKey:@"currPinColor"]; 
    NSLog(@"Pin color: %d", [defaults integerForKey:@"currPinColor"]); 
    annView.animatesDrop=TRUE; 
    annView.canShowCallout = YES; 
    annView.calloutOffset = CGPointMake(-5, 5); 
    return annView; 
} 

"if"블록의 NSLog 문은 항상 색이 설정되었음을 확인하지만 대리자는 경우에 따라 항상 올바른 색으로 끝나는 것은 아닙니다. 또한 일반적으로 새로운 위치를 검색하면 "if"블록의 마지막 색상으로 모든 핀이 설정되지만 동일한 위치를 다시 검색하면 핀이 올바른 색상으로 설정됩니다.

나는이 방법으로 NSUserDefaults를 사용하지 않을 것으로 생각하지만, "currentPinColor"라는 추가 속성을 포함하는 MKAnnotation에 대한 내 자신의 하위 클래스를 만들려고했는데,이 동안 "currentPinColor"를 설정할 수있었습니다. 나는 delegate 메소드에서 "currentPinColor"에 접근하려고 시도했다. 컴파일러는 MKAnnotation과 관련하여 "currentPinColor"에 대해 아무것도 모른다고 불평했다. 공정한 충분히 내가 대리자 방법을 수정하려고 다음 생각, 대신 기본의


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


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

하는 컴파일러가 그것을위한 프로토콜에 대해 아무것도 몰랐다 불평 포인트 이 위임 컨텍스트의 MyCustomMKAnnotation입니다.

위임 메서드 및/또는 MyCustomMKAnnotation을 설정하는 올바른 방법은 무엇입니까? 일관된 핀 색상 설정을 얻는 데 적합한 방법은 무엇입니까? 나는 사물에 대한 아이디어를 여기에서 시도하는 것에 관한 것입니다.

답변

3

나는 내 자신의 문제를 해결했습니다. 때때로 질문을 공식화하는 것으로 충분합니다! 문제는 내 MKAnnotation 대리자에 액세스하려는 방식이었습니다. 나는이 두 가지 관련 게시물에 무슨 일이 있었 게시 후

,

Mapkit issue in finding annotation current position

사용자 포격에 의한 첫 번째 게시물에 대한 답변,

MKPinAnnotationView: Are there more than three colors available?

그러나


- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation 
{ 
    // ... get the annotation delegate and allocate the MKAnnotationView (annView) 
    if ([annotationDelegate.type localizedCaseInsensitiveCompare:@"NeedsBluePin"] == NSOrderedSame) 
    { 
     UIImage * image = [UIImage imageNamed:@"blue_pin.png"]; 
     UIImageView *imageView = [[[UIImageView alloc] initWithImage:image] autorelease]; 
     [annView addSubview:imageView]; 
    } 
    // ... 

내 문제를 제시 나는 그 대리인을 얻는 방법을 몰랐고 그것은 어딘가에 떠돌았다. 위 스 니펫의 일부입니다. 제 포스트에 대한 답 또한, 사용자에 의해 포격이 두 단편을 함께 원하는 결과를 생성하고 결국 내 문제를 해결 퍼팅이없는 비트


- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control 
{ 
    AnnotationDelegate * delegate = (AnnotationDelegate*)view.annotation; 
    // do stuff with delegate.position; 
} 

을 채웠다.

근로 viewForAnnotation 위임 방법은 다음과 같습니다

,


- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { 
    if(annotation == mapView.userLocation){ return nil; } 
    AddressAnnotation *delegate = annotation; //THIS CAST WAS WHAT WAS MISSING! 
    MKPinAnnotationView *annView = nil; 
    annView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"currentloc"]; 
    if(annView == nil){ 
     annView = [[MKPinAnnotationView alloc] initWithAnnotation:delegate reuseIdentifier:@"currentloc"]; 
    } 

    if ([delegate.classPinColor localizedCaseInsensitiveCompare:@"green"] == NSOrderedSame) { 
     annView.pinColor = MKPinAnnotationColorGreen; 
    }else if ([delegate.classPinColor localizedCaseInsensitiveCompare:@"purple"] == NSOrderedSame){ 
     annView.pinColor = MKPinAnnotationColorPurple; 
    }else if ([delegate.classPinColor localizedCaseInsensitiveCompare:@"red"] == NSOrderedSame){ 
     annView.pinColor = MKPinAnnotationColorRed; 
    } 
    annView.animatesDrop=TRUE; 
    annView.canShowCallout = YES; 
    annView.calloutOffset = CGPointMake(-5, 5); 
    return annView; 
} 

는 희망이 다른 사람에게 도움이 될 것입니다.

감사합니다. Cannonade!

+0

왜 내 질문에 답변하는 데 2 ​​일이 걸립니까? – si28719e

+0

3 번 줄에있는 'AddressAnnotation'은 무엇입니까? –

관련 문제