Swift 如何在一个MapView中使用不同的视图
如何在一个地图视图中使用不同的视图。我的意思是如果我使用Swift 如何在一个MapView中使用不同的视图,swift,annotations,mapkit,Swift,Annotations,Mapkit,如何在一个地图视图中使用不同的视图。我的意思是如果我使用 func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? 然后,所有注释视图在一个地图视图中都将相同。 我的意思是如何在一个地图视图中设置不同的视图样式(包括图像等)?您可以使用不同的视图。比如说, func mapView(_ mapView: MKMapView, viewFor annotation: M
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?
然后,所有注释视图在一个地图视图中都将相同。
我的意思是如何在一个地图视图中设置不同的视图样式(包括图像等)?您可以使用不同的视图。比如说,
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
return nil
}
if ... {
// Marker annotation
let reuseId = "pin"
var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKMarkerAnnotationView
if pinView == nil {
pinView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView?.canShowCallout = true
pinView?.isDraggable = true
let rightButton: AnyObject! = UIButton(type: UIButtonType.detailDisclosure)
pinView?.rightCalloutAccessoryView = rightButton as? UIView
}
else {
pinView?.annotation = annotation
}
return pinView
} else if ... {
// Image annotation
let reuseId = "image"
var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId)
if pinView == nil {
pinView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView?.canShowCallout = true
pinView?.image = UIImage(named: "Laugh")
let rightButton = UIButton(type: UIButtonType.detailDisclosure)
pinView?.rightCalloutAccessoryView = rightButton
}
else {
pinView?.annotation = annotation
}
return pinView
}
}