Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change selected pin image in Map Kit in Swift 3?

I have a map and on this map I have custom annotation pins. All pins have same custom image. When i click on a pin, i need to change this annotation's image. I was using Google Maps before:

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
}

This was working fine. But I dont know how to do it with MapKit. I want to change just selected marker(pin) image. How can I do this?

Also I tried this but not working How to change non selected annotation pin images on mapkit with Swift

And this is my code:

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!)

      }
}
like image 497
Hilalkah Avatar asked Oct 26 '17 07:10

Hilalkah


1 Answers

"selected" means "tapped"? If so, Try the following code:

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")
}
like image 192
Kosuke Ogawa Avatar answered Nov 15 '22 16:11

Kosuke Ogawa