0
在我的代码中,我正在使用Apple地图,并在View Controller中正确设置了mapView和delegate。为什么我无法自定义我的MKAnnotationView?
在我的ViewController我也有内部的下面的代码循环:
if userOnMapAlready == false {
let annotation = MKPointAnnotation()
annotation.coordinate = CLLocationCoordinate2D(latitude: otherUserLocation.coordinate.latitude, longitude: otherUserLocation.coordinate.longitude)
annotation.subtitle = otherUserUUID
self.mapView.addAnnotation(annotation)
}
我也有我的视图控制器下面的委托功能,并重新设置我的视图控制器内mapview.delegate =自:
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
//return nil so map view draws "blue dot" for standard user location
return nil
}
let reuseId = "pin"
var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKPinAnnotationView
if pinView == nil {
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView!.canShowCallout = true
pinView!.animatesDrop = true
pinView!.pinColor = .purple
}
else {
pinView!.annotation = annotation
}
return pinView
}
可悲的是,当销得出他们都是红色,紫色不一样viewForAnnotation会建议他们应。问题是什么?!这是否与重用ID不匹配分配给每个MKPointAnnotation的唯一字幕?
更新:刚刚意识到,委托函数viewForAnnotation永远不会被调用 - 这将解释为什么引脚不是紫色。为什么viewForAnnotation不会被调用?
我想这种变化,但它仍然没有工作。实际上,我基于printForAnotation并没有被调用 - 因此是问题。任何想法为什么它不被称为? –
您可能需要启用它(例如'mapView.view(用于:注释)?。isEnabled = enabled')... –