2011-01-11 9 views
3

私はカスタムの注釈吹き出しを表示するためにこのtutorialをフォローしてきました。 カスタムアノテーションビューを持つ注釈が1つしかない場合は、完全に機能します。カスタム吹き出しからMKMap内の別の吹き出しにジャンプ

しかし、私はマップ上にもっと多くのものを置く必要があります。私はカスタム注釈ビューから別のビューに切り替えるのに苦労しています。すでに1つを選択して別のピンをクリックして、新しいカスタムアノテーションビューを表示したい場合、それは機能しません。私は最初にmapviewのどこか別の場所をクリックする必要があります。私はDidDeselectメソッドで作業するものがあると思いますが、わかりません...

どうやってこのような問題を解決しますか?

- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view { 
    if (self.calloutAnnotation && [view.annotation isKindOfClass:[MyHomeAnnotation class]]) { 
     [self.mapView removeAnnotation: self.calloutAnnotation]; 
    } 
} 
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view { 
    if ([view.annotation isKindOfClass:[MyHomeAnnotation class]]) { 
     if (self.calloutAnnotation == nil) { 
      self.calloutAnnotation = [[CalloutMapAnnotation alloc] initWithLatitude:view.annotation.coordinate.latitude 
                     andLongitude:view.annotation.coordinate.longitude]; 
     } else { 
      self.calloutAnnotation.latitude = view.annotation.coordinate.latitude; 
      self.calloutAnnotation.longitude = view.annotation.coordinate.longitude; 
     } 
     [self.mapView addAnnotation:self.calloutAnnotation]; 
     self.selectedAnnotationView = view; 
    } 
} 



- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { 
    if (annotation == self.calloutAnnotation) { 
     CalloutMapAnnotationView *calloutMapAnnotationView = (CalloutMapAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:@"CalloutAnnotation"]; 
     if (!calloutMapAnnotationView) { 
      calloutMapAnnotationView = [[[CalloutMapAnnotationView alloc] initWithAnnotation:annotation 
                      reuseIdentifier:@"CalloutAnnotation"] autorelease]; 
      calloutMapAnnotationView.contentHeight = 78.0f; 
      UIImage *asynchronyLogo = [UIImage imageNamed:@"cykelrød1.png"]; 
      UIImageView *asynchronyLogoView = [[[UIImageView alloc] initWithImage:asynchronyLogo] autorelease]; 
      asynchronyLogoView.frame = CGRectMake(5, 2, asynchronyLogoView.frame.size.width, asynchronyLogoView.frame.size.height); 
      [calloutMapAnnotationView.contentView addSubview:asynchronyLogoView]; 
     } 
     calloutMapAnnotationView.parentAnnotationView = self.selectedAnnotationView; 
     calloutMapAnnotationView.mapView = self.mapView; 
     return calloutMapAnnotationView; 
    } else if ([annotation isKindOfClass:[MyHomeAnnotation class]]) { 
     MKPinAnnotationView *annotationView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation 
                       reuseIdentifier:@"CustomAnnotation"] autorelease]; 
     annotationView.canShowCallout = NO; 
     annotationView.pinColor = MKPinAnnotationColorGreen; 
     return annotationView; 
    }else if ([annotation isKindOfClass:[MyMapAnnotation class]]) { 
     MKPinAnnotationView *annotationView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation 
                       reuseIdentifier:@"NormalAnnotation"] autorelease]; 
     annotationView.canShowCallout = YES; 
     annotationView.pinColor = MKPinAnnotationColorPurple; 
     return annotationView; 
    } 


    return nil; 
} 
+0

私は本当に多くMKMapフレームワークを使用していないので、私は本当に私が言うことができるすべては、あなたが 'MKMapView'クラスのサブビューを作ることができている....あなたを助けることができない、他の場所にタップをシミュレートマップビューを作成し、それをスーパービューに渡しますか?それは実際にはないときに以前のタップがあったと思うようになります.... –

+0

これは私が何をすべきかのように聞こえますが、私はそれを行う方法について全く考えていません... – danskcollignon

答えて

4

ここで私の解決策は、緯度と経度を使用して、どの吹き出しをマップビューから削除するかを区別することです。これが助けになることを願っています。

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view { 
    NSLog(@"didSelectAnnotationView:%f", view.annotation.coordinate.latitude); 

    if (self.calloutAnnotation == nil) { 

     CalloutMapAnnotation *tempCallout = [[CalloutMapAnnotation alloc] 
             initWithLatitude:view.annotation.coordinate.latitude    
             andLongitude:view.annotation.coordinate.longitude]; 

     self.calloutAnnotation = tempCallout; 
     [tempCallout release]; 

    } else { 

     //remove callout when callout already exist 
     [self.myMapView removeAnnotation: self.calloutAnnotation]; 
     self.selectedAnnotationView = nil; 

     //reposition 
     self.calloutAnnotation.latitude = view.annotation.coordinate.latitude; 
     self.calloutAnnotation.longitude = view.annotation.coordinate.longitude; 

    } 

    [self.myMapView addAnnotation:self.calloutAnnotation]; 
    self.selectedAnnotationView = view; 
} 

- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view { 

    NSLog(@"didDeselectAnnotationView:%f", view.annotation.coordinate.latitude); 

    //use the latitude and longitude to avoid remove twice 

    if (self.calloutAnnotation && 
    self.selectedAnnotationView.annotation.coordinate.latitude == view.annotation.coordinate.latitude && 
    self.selectedAnnotationView.annotation.coordinate.longitude == view.annotation.coordinate.longitude 
    ) { 

     [self.myMapView removeAnnotation: self.calloutAnnotation]; 
     self.selectedAnnotationView = nil; 
     self.calloutAnnotation.isAddtoMap = NO; 
    } 

} 
+0

私のために働く!ありがとう! –

関連する問題