如何在Swift 3中更改Map Kit中的选定图像?

Hil*_*kah 2 google-maps annotations mapkit ios swift

我有一张地图,在这张地图上我有自定义注释引脚.所有引脚都有相同的自定义图像 当我点击一个图钉时,我需要更改这个注释的图像.我之前使用的是谷歌地图:

func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
    if marker.userData as? Int != nil {
        let marker_tag = marker.userData as! Int
        Datas.selectedMarkerIndex = marker_tag

        if let selectedMarker = mapView.selectedMarker {
            selectedMarker.icon = UIImage(named: "marker_gray")
        }
        mapView.selectedMarker = marker
        marker.icon = UIImage(named: "marker_red")                        
    }
    return true
}
Run Code Online (Sandbox Code Playgroud)

这工作正常.但我不知道如何使用MapKit.我想改变选择的标记(图钉)图像.我怎样才能做到这一点?

我也尝试了这个,但没有工作 如何使用Swift在mapkit上更改非选定的注释图像

这是我的代码:

class CustomPointAnnotation: MKPointAnnotation {
    var pinImageName:String!
    var userData:Int!
}

extension MyView: MKMapViewDelegate {

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    let reuseIdentifier = "my_pin"
    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseIdentifier)

    if annotationView == nil {
        annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)

    } else {
        annotationView?.annotation = annotation
    }

    let customPointAnnotation = annotation as! CustomPointAnnotation
    annotationView?.isDraggable = false
    annotationView?.canShowCallout = false
    annotationView?.layer.anchorPoint = CGPoint(x: 0.5, y: 1)
    annotationView?.image = UIImage(named: customPointAnnotation.pinImageName)

    return annotationView
}

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    if let anno = view.annotation as? CustomPointAnnotation {            
        let marker_tag = anno.userData!
        print(marker_tag)

    }
}

func addMarkersOnMap(){
    if Datas.myArray.count != 0 {
        Datas.selectedMarkerIndex = 0
        for i in 0..<Datas.myArray.count{
            let lat = Datas.myArray[i].lat
            let lng = Datas.myArray[i].lng

            myArray.append(CustomPointAnnotation())                
            if lat != nil {                    
                let location = CLLocationCoordinate2D(latitude: lat!, longitude: lng!)
                myArray[i].pinImageName = "marker_gray"
                myArray[i].coordinate = location
                myArray[i].userData = i
                pinAnnotationView = MKPinAnnotationView(annotation: myArray[i], reuseIdentifier: "my_pin")
                mapView.addAnnotation(pinAnnotationView.annotation!)

      }
}
Run Code Online (Sandbox Code Playgroud)

Kos*_*awa 6

"选中"是指"轻拍"?如果是这样,请尝试以下代码:

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    view.image = UIImage(named: "marker_gray")
}

func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) {
    view.image = UIImage(named: "marker_red")
}
Run Code Online (Sandbox Code Playgroud)

  • 谢谢!这太容易了。如果我想在外面选择,我使用了这个代码:mapView.selectAnnotation(myArray[i],animated: true) (2认同)