ios - Polyline won't show up in MapKit -
i'm using xcode 8.3.2 first import mapkit. set markers map. add following code add polyline map won't show any.
class viewcontroller: uiviewcontroller, mkmapviewdelegate { @iboutlet weak var mapview: mkmapview!
override func viewdidload() { self.mapview.delegate = self super.viewdidload() let template = "http://tile.openstreetmap.org/{z}/{x}/{y}.png" let point1 = cllocationcoordinate2d(latitude: 6.9271, longitude: 79.8612); let point2 = cllocationcoordinate2d(latitude: 9.6615, longitude: 80.0255); let overlay = mktileoverlay(urltemplate: template) overlay.canreplacemapcontent = true let location = cllocationcoordinate2dmake(6.878069, 79.892119) mapview.add(overlay, level: .abovelabels) mapview.setregion(mkcoordinateregionmakewithdistance(location, 1100, 1100), animated: true) let pin = pinannotation(title: "nimbus", subtitle: "best", coordinate: location) mapview.addannotation(pin) let points: [cllocationcoordinate2d] points = [point1, point2] let polyline = mkgeodesicpolyline(coordinates: points, count: 3) mapview.add(polyline) uiview.animate(withduration: 1.5, animations: { () -> void in let span = mkcoordinatespanmake(0.01, 0.01) let region1 = mkcoordinateregion(center: point1, span: span) self.mapview.setregion(region1, animated: true) }) } func mapview(_ mapview: mkmapview, rendererfor overlay: mkoverlay) ->mkoverlayrenderer{ if let overlaygeodesic = overlay as? mkgeodesicpolyline { let overlayrenderer = mkpolylinerenderer(polyline: overlaygeodesic) overlayrenderer.linewidth = 5 overlayrenderer.strokecolor = uicolor.blue return overlayrenderer } return mkoverlayrenderer(overlay: overlay) }
first need add line, think have added anyway
self.mapview.delegate = self
after need implement mkmapviewdelegate
method func mapview(_ mapview: mkmapview, rendererfor overlay: mkoverlay) -> mkoverlayrenderer
, return mkoverlayrenderer
needed current overlay in case mkpolylinerenderer
important part if don't implement method never have polyline rendered
implementation this
func mapview(_ mapview: mkmapview, rendererfor overlay: mkoverlay) -> mkoverlayrenderer { if let overlaygeodesic = overlay as? mkgeodesicpolyline { let overlayrenderer = mkpolylinerenderer(polyline: overlaygeodesic) overlayrenderer.linewidth = 5 overlayrenderer.strokecolor = uicolor.blue return overlayrenderer } if let overlaytile = overlay as? mktileoverlay{ let overlayrenderer = mktileoverlayrenderer(tileoverlay: overlaytile) return overlayrenderer } return mkoverlayrenderer(overlay: overlay) }
and voila! there polyline rendered
wiki
Comments
Post a Comment