2016-05-07 20 views
0

我正在使用mkmapview將應用程序放置到mapview上。如何識別來自viewForAnnotation的XCode MKMapView引腳

我需要能夠根據有關引腳的信息對引腳着色。

那滴地圖管腳的當前代碼:

- (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; 
} 

如果我改變行:

customPin.pinColor = MKPinAnnotationColorRed; 

然後我就可以改變所有的下降銷的顏色,但我怎麼能識別哪個引腳正在被丟棄,以便我只能在需要時重新對引腳着色?

我加了日誌行:

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語句一樣簡單)。