1

I want to change marker image in my MKPointAnnotation() and I have successfully changed my image but problem is that I have 2 marker points and I want to place ignitionon.png on point one and ignitionof.png on point two and my logic place ignitionon.png on both the points.

Code

if(self.doubelLocation) {
            var pointOff =  CLLocationCoordinate2D()
            var pointOn  = CLLocationCoordinate2D()
            if(self.dateTime.count > 0) {
                for(var i : Int = 0 ; i < self.dateTime.count ; i++) {
                    // print("Real status = \(self.dateTime[i])")
                    var fixTime = self.dateTime[i]
                    if(fixTime == self.dateTimeTwo) {
                        pointOff = CLLocationCoordinate2DMake(self.latt[i], self.lngg[i]);
                        print("pointOff = \(pointOff)")
                        //points.append(pointOf)
                    }
                    if(fixTime == self.dateTimeOne){

                        pointOn = CLLocationCoordinate2DMake(self.latt[i], self.lngg[i]);

                        print("pointOn = \(pointOn)")
                        //points.append(pointOf)

                    }

                }
                var points: [CLLocationCoordinate2D]

                points = [pointOn, pointOff]

                dispatch_async(dispatch_get_main_queue(), {
                    let geodesic = MKGeodesicPolyline(coordinates: &points[0], count: 2)
                    self.theMapView.addOverlay(geodesic)
                    let latDelta:CLLocationDegrees = 0.03
                    let lnggDelta:CLLocationDegrees = 0.03

                    UIView.animateWithDuration(1.5, animations: { () -> Void in
                        let span = MKCoordinateSpanMake(latDelta, lnggDelta)
                        let region1 = MKCoordinateRegion(center: points[0], span: span)
                        self.theMapView.setRegion(region1, animated: true)
                        self.ActivityIndicator.stopAnimating()


                        for(var i : Int = 0 ;i < points.count; i++){

                            var st = self.reportText[i]



                         //   let theMarker = MKPointAnnotation()
                            //how to change marker color
                            //https://stackoverflow.com/questions/33532883/add-different-pin-color-with-mapkit-in-swift-2-1

                        let theMarker = MKPointAnnotation()
                            theMarker.coordinate = points[i]



                          //  if(st == "IGNITION ON"){
                            if(i == 0){

                                theMarker.title = "Status : IGNITION OFF"
                                theMarker.subtitle = "\(self.locationOff)"
                                // theMarker.subtitle = "Date = , Reg#:  "
                                self.theMapView.addAnnotation(theMarker)

                                let anView1:MKAnnotationView = MKAnnotationView()
                                anView1.annotation = theMarker
                                anView1.image = UIImage(named:"ignitionof")
                                anView1.canShowCallout = true
                                anView1.enabled = true
                            }
                            if(i == 1){

                              //  theMarker = UIColor.greenColor()
                                theMarker.title = "Status : IGNITION ON"
                                theMarker.subtitle = "\(self.locationOn)"

                                // theMarker.subtitle = "Date = , Reg#:  "
                                self.theMapView.addAnnotation(theMarker)

                                //how to change image of marker 
                                //https://stackoverflow.com/questions/24467408/swift-add-mkannotationview-to-mkmapview

                                let anView:MKAnnotationView = MKAnnotationView()
                                anView.annotation = theMarker
                                anView.image = UIImage(named:"ignitionon")
                                anView.canShowCallout = true
                                anView.enabled = true
                            }
                         //   }



                        }
                    })

                })

            }


        }

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {

    if (annotation is MKUserLocation) {
        //if annotation is not an MKPointAnnotation (eg. MKUserLocation),
        //return nil so map draws default view for it (eg. blue dot)...
        return nil
    }

    let reuseId = "test"

    var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
    if anView == nil {
        anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        anView!.image = UIImage(named:"ignitionon")
        anView!.canShowCallout = true
    }
    var anView1 = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
    if anView1 == nil {
        anView1 = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        anView1!.image = UIImage(named:"ignitionof")
        anView1!.canShowCallout = true
    }
    else {
        //we are re-using a view, update its annotation reference...
        anView!.annotation = annotation
    }

    return anView
}

I am following this Link: Swift - Add MKAnnotationView To MKMapView

Community
  • 1
  • 1
zeeshan nazakat
  • 151
  • 3
  • 18

1 Answers1

0

It's not the best way to handle multiple marker with different metadata. You can't use mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) two times or more because in viewForAnnotation it is for only 1 view for each Point you have added in the stack.

Create a sub-class of MKPointAnnotation:

class CustomPointAnnotation: MKPointAnnotation {

    var tag: String!

}

Create a Dictionary with all images:

var imagesPath : ["tag_1": "image_1.png", "tag_2": "image_2.jpg"]

Now in the delegate func, check Simply

if !(annotation is CustomPointAnnotation) {
            return nil
        }

and Handle the only one View you have:

let reuseId = "test"

var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
if anView == nil {
    anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
   var customannotation = annotation as! CustomPointAnnotation
        anView!.image = UIImage(named: imagesPath(customannotation.tag))
    anView!.canShowCallout = true
}

An example to add a new Custom point is:

let aPoint = CustomPointAnnotation()
    aPoint.coordinate = CLLocationCoordinate2DMake(40.730872, -73.003066)
    aPoint.title = "Info1"
    aPoint.subtitle = "Subtitle"
    aPoint.tag = "tag_1"

    mapView.addAnnotation(aPoint)
Patonz
  • 884
  • 1
  • 6
  • 17