2016-05-07 1 views
0

mmapapviewを使用してアプリケーションを開発しています。これはピンをmapviewに落とします。viewForAnnotationからXCode MKMapViewピンを特定する方法

ピンに関する情報に基づいてピンを色付けできる必要があります。

地図上のアイコンをドロップし、現在のコードは次のとおりです。

customPin.pinColor = MKPinAnnotationColorRed; 

その後、私はすべてドロップされたピンの色を変更することができますが、どのようにすることができます:私は、行を変更した場合

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation { 
if (annotation == self.mapView.userLocation) return nil; 
NSLog(@"annotation = %@", annotation); 
static NSString* AnnotationIdentifier = @"AnnotationIdentifier"; 
MKPinAnnotationView* customPin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier]; 
customPin.pinColor = MKPinAnnotationColorRed; 
customPin.animatesDrop = YES; 
customPin.canShowCallout = YES; 
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
customPin.rightCalloutAccessoryView = rightButton; 
return customPin; 
} 

どのピンがドロップされているかを識別し、必要に応じてピンの色を変更するだけです。

私は、ログの行を追加:

NSLog(@"annotation = %@", annotation); 

をしかし、それは例えば、返されます。

annotation = <MapAnnotation: 0x7feabd749190> 
annotation = <MapAnnotation: 0x7feac04edf50> 
annotation = <MapAnnotation: 0x7feabd79f860> 

は、どのように私はピンを識別するためにこれを使用することができますか?

注釈ピンを別の場所に塗りつぶす必要がありますか?

+0

アノテーションを追加するときにカスタムアノテーションクラスを作成しましたか? –

+0

これを確認してくださいhttps://www.raywenderlich.com/90971/introduction-mapkit-swift-tutorial –

答えて

0

MKAnnotationプロトコルに準拠するオブジェクトを注釈としてマップに追加できます。

私はあなたはそれがユーザーの位置注釈ではありませんことを確認したら、あなたのviewForAnnotation方法に続いて(例えば、ピンタイプの列挙など)

を余分な性質を持つカスタム注釈オブジェクトを作成し提案し、キャストidポインタを使用してカスタムアノテーションオブジェクトのタイプを確認し、表示するピンのタイプを確認します(switch文と同じように簡単です)。

関連する問題