Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何在一个MapView中使用不同的视图_Swift_Annotations_Mapkit - Fatal编程技术网

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