2014-05-19 1 views
1

코드에 주석 (주석)을 추가 할 수있는지도가 하나 있습니다. 내 앱을 실행할 때 내 앱에서 주석을 추가 할 때이 주석이 자동으로 선택되고지도 대신 주석을 클릭하면 내 주석이 선택 취소됩니다. 선택을 처리하고 주석을 선택 취소 할 수 있지만 필히 주석을 클릭하고지도 (주석 대신 다른 위치)를 클릭 할 때까지 반드시 주석을 선택 취소해야합니다.지도 키트에 추가 할 때 주석 자동 선택 방법

나는 이것을 원하지 않는다. 내 앱을 실행하면 내 특수 효과가 자동으로 선택되고 내 특수 효과의 선택이 취소 될 때까지지도의 다른 장소를 클릭해야합니다.

안내해주세요. 이것은 선택/선택 해제 주석 처리를위한 코드입니다.

static NSString* const ANNOTATION_SELECTED_DESELECTED = @"mapAnnotationSelectedOrDeselected"; 


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { 
    NSLog(@"2"); 
    NSString *action = (__bridge NSString *)context; 
    if ([action isEqualToString:ANNOTATION_SELECTED_DESELECTED]) { 
     BOOL annotationSelected = [[change valueForKey:@"new"] boolValue]; 
     if (annotationSelected) { 
      NSLog(@"Annotation was selected, do whatever required"); 
     }else { 
      NSLog(@"Annotation was deselected, do what you must"); 

     } 
    } 
} 
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views { 
    if ([mapViews.annotations count] > 1) { 
     for (MKAnnotationView *anAnnotationView in views) { 
      [anAnnotationView addObserver:self forKeyPath:@"selected" options:NSKeyValueObservingOptionNew context:(__bridge void *)(ANNOTATION_SELECTED_DESELECTED)]; 
     } 
    } 

} 
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { 
    MKPinAnnotationView *pinView = (MKPinAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:@"Prospects"]; 
    if ([annotation isKindOfClass:[MKUserLocation class]]){ 
     return nil; //return nil to use default blue dot view 
    } 
    else if(pinView == nil) { 

     pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Prospects"]; 
     pinView.pinColor = MKPinAnnotationColorPurple; 
     pinView.animatesDrop = YES; 
     pinView.draggable = NO; 
    } 
    return pinView; 
} 

답변

6

지정된 주석을 선택하고 그에 대한 설명보기를 표시합니다. 이 방법은 효과가 없습니다,

[mapView selectAnnotation:pinView animated:YES]; 여기 pinView은 주석입니다 // 지정된 주석이 관련 주석 뷰가없는 때문에 화면이 아니며, 경우지도보기는지도

입니다.

내 주석이 선택 해제 될 때까지지도의 다른 위치를 클릭하십시오. mapview의 기본 동작입니다.

MKMapView Reference

opening-callout-of-annotation-on-map-automatically-when-map-first-load

+1

와 같은 솔루션이 오류를 얻을 : 오류 : 당신이 후에이 작업을 수행 할 필요가 – user3599133

+0

을 추가되지 않은 주석을 선택하려고 [mapView addAnnotation : pinView]를 실행 한 후지도에 주석을 추가합니다. – Pawan

+0

내 친구에게 감사 : D – user3599133

0

빠른 3

func mapView(_ mapView: MKMapView, didAdd views: [MKAnnotationView]) { 
    self.mapView.selectAnnotation(self.mapView.annotations[0], animated: true) 
}