2012-05-02 2 views
3

사용자가지도를 터치 할 때 주석 추가에 몇 가지 문제가 있습니다.MapKit 및 UIGestureRecognizer를 사용하여 터치시 주석 핀 추가

나는 사용자의 터치를 감지하기 위해 UIGestureRecognizer을 사용하고 있습니다. 길게 누르면이 감지되면

는,이 함수를 호출하고 있습니다 :

- (void)handleLongPressGesture:(UIGestureRecognizer*)gestureRecognizer{ 
if (gestureRecognizer.state == UIGestureRecognizerStateEnded) return; 

NSLog(@"long press"); 

CGPoint touchPoint = [gestureRecognizer locationInView:mapView]; 
CLLocationCoordinate2D touchMapCoordinate = 
[mapView convertPoint:touchPoint toCoordinateFromView:mapView]; 

RdvAnnotation *rdvAnnotation = [[RdvAnnotation alloc] init]; 

[rdvAnnotation initWithCoordinate:touchMapCoordinate]; 

[mapView removeAnnotations:mapView.annotations]; 

[mapView addAnnotation:rdvAnnotation]; 

[rdvAnnotation release]; } 

I 캔은 콘솔에서 NSLog를 확인하고 rdvAnnotation는 좌표 좋은 초기화됩니다.

지도에서 내 특수 효과를 볼 수없는 이유를 알 수 없습니다.

여기 내 - (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation 방법입니다 :

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation{ 
if ([annotation isKindOfClass:[MKUserLocation class]]) 
    return nil; 

if ([annotation isKindOfClass:[RdvAnnotation class]]) 
{ 
    static NSString* RdvAnnotationIdentifier = @"rdvAnnotationIdentifier"; 
    MKPinAnnotationView* pinView = (MKPinAnnotationView *) 
    [mapView dequeueReusableAnnotationViewWithIdentifier:RdvAnnotationIdentifier]; 

    if (!pinView) 
    { 
     MKPinAnnotationView* customPinView = [[[MKPinAnnotationView alloc] 
               initWithAnnotation:annotation reuseIdentifier:RdvAnnotationIdentifier] autorelease]; 
     customPinView.pinColor = MKPinAnnotationColorPurple; 
     customPinView.animatesDrop = YES; 
     customPinView.canShowCallout = YES; 

     return customPinView; 

    } 
    else 
    { 
     pinView.annotation = annotation; 
    } 
    return pinView; 
} 
return nil;} 

viewDidLoad 방법 : 당신은 두 번 초기화 호출하고

RdvAnnotation *rdvAnnotation = [[RdvAnnotation alloc] init]; 
[rdvAnnotation initWithCoordinate:touchMapCoordinate]; 

: 난 그냥 이상한 것 같다 뭔가를 발견

- (void)viewDidLoad { 
[super viewDidLoad]; 

mapView = [[MKMapView alloc] initWithFrame:self.view.frame]; 
[mapView setShowsUserLocation:TRUE]; 
[mapView setMapType:MKMapTypeStandard]; 
[mapView setDelegate:self]; 

CLLocationManager *locationManager=[[CLLocationManager alloc] init]; 

[locationManager setDelegate:self]; 

[locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters]; 

[locationManager startUpdatingLocation]; 

self.navigationItem.title = @"Rendez-vous" ;  
} 
+0

'- (MKAnnotationView *) mapView : (MKMapView *) mapView viewForAnnotation : (id ) annotation' MapView delegate을 구현 했습니까? 실제보기가 생성됩니다. – adig

+1

예, 질문을 업데이트했습니다. 뭐가 잘못 보이니? – Serviet

+0

괜찮을 것 같습니다. 그것은 좌표 변환에 제스처 위치와 관련이있을 수 있습니까? 좌표가지도 뷰의 표시 영역에 있는지 확인할 수 있습니까? – adig

답변

0

주석 객체.

RdvAnnotation *rdvAnnotation = [[RdvAnnotation alloc] initWithCoordinate:touchMapCoordinate]]; 

편집 : 당신이 좌표 속성 값을 잃게 초기화를 유지하고 변경하지 않으려는 init 메소드에서 코드가있는 경우 :

당신은 이런 식으로해야
RdvAnnotation *rdvAnnotation = [[RdvAnnotation alloc] init]; 
rdvAnnotation.coordinate = touchMapCoordinate; 
+1

이 오류가 수정되었지만 문제가 해결되지 않았습니다. 아직 내 새로운 주석이 없습니다. 사실, 첫 번째 초기화는 내 객체를 초기화하고 두 번째는 내 객체에 올바른 좌표를 배치합니다. – Serviet

+0

오버로드 된 init에 중요한 코드가 있다면, 내가 편집에서 제안한 것을 해답으로 시도해보십시오. – adig

+1

그걸로 또한 시도했지만, 여전히 아무것도. 그러나, 나는 당신의 도움에 매우 감사합니다. – Serviet

0

viewDidLoad에서 새 맵보기 개체를 만듭니다.

먼저이 새로운지도보기 개체는 보조보기로 self.view에 추가되지 않으므로 메모리에 있지만 보이지 않습니다.

둘째,지도 작성기 개체가 인터페이스 작성기의보기에 이미 배치되어 있으므로 새로 만들 필요가 없습니다.

아마도 어노테이션을 추가 할 때 주석이 메모리의 맵보기 오브젝트에는 추가되지만 표시되는 것은 보이지 않습니다 (IB에서 작성된 것).

viewDidLoad에 새 맵보기를 만드는 대신 mapView IBOutlet을 Interface Builder의 맵보기에 연결하십시오.

+1

그래, ViewDidLoad에서 mapView에 대한 선언을 제거했습니다. IB에서 mapView가 잘 연결되어 있지만 새로운 특수 효과를 볼 수 없습니다. – Serviet

+0

.h에서 mapView는'@property (retain) IBOutlet MKMapView * mapView;로 정의되어 있고, IB에서는 맵 뷰에 연결된 File 's Owner의'mapView' 아울렛입니까? 제스처 인식기의 NSLog가 계속 표시되고 카운트가 표시됩니까? – Anna

+1

에서 .h mapView는 @property (retain)로 정의됩니다. IBOutlet MKMapView * mapView; IB에서는 mapView 콘센트가 맵보기에 연결됩니다. 지도의 언론 (내가 removeAnnotation을 제거했습니다) 때 카운트가 증가합니다. – Serviet