swift2 - how to place two images on two different markers in mapKit using swift -


i want change marker image in mkpointannotation() , have changed image problem have 2 marker points , want place ignitionon.png on point 1 , ignitionof.png on point 2 , logic place ignitionon.png on both points.

code

if(self.doubellocation) {             var pointoff =  cllocationcoordinate2d()             var pointon  = cllocationcoordinate2d()             if(self.datetime.count > 0) {                 for(var : int = 0 ; < 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 : int = 0 ;i < points.count; i++){                              var st = self.reporttext[i]                             //   let themarker = mkpointannotation()                             //how 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 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 mkuserlocation) {         //if annotation not mkpointannotation (eg. mkuserlocation),         //return nil map draws default view (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 re-using view, update annotation reference...         anview!.annotation = annotation     }      return anview } 

i following link: swift - add mkannotationview mkmapview

it's not best way handle multiple marker different metadata. can't use mapview.dequeuereusableannotationviewwithidentifier(reuseid) 2 times or more because in viewforannotation 1 view each point have added in stack.

create sub-class of mkpointannotation:

class custompointannotation: mkpointannotation {      var tag: string!  } 

create dictionary images:

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

now in delegate func, check simply

if !(annotation custompointannotation) {             return nil         } 

and handle 1 view 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 add 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) 

Comments