ios - I cannot get my MKRoute to show up on a MapView. I'm very stuck -
i trying add mkoverlay (my mkroute) mapview, cannot figure out doing wrong. pretty sloppy because i'm experimenting before implement in app.
here's code:
import uikit import mapkit import corelocation class viewcontroller: uiviewcontroller, mkmapviewdelegate, cllocationmanagerdelegate { @iboutlet var map: mkmapview! var geocoder:clgeocoder = clgeocoder() var location:cllocation = cllocation(latitude: 38, longitude: -77) var next:cllocation = cllocation(latitude: 38.9, longitude: -77.61) var locmark:mkplacemark? var destmark:mkplacemark? var manager:cllocationmanager = cllocationmanager() var source:mkmapitem? var destination:mkmapitem? var request:mkdirectionsrequest = mkdirectionsrequest() var directions:mkdirections = mkdirections() var directionsresponse:mkdirectionsresponse = mkdirectionsresponse() var route:mkroute! = mkroute() override func viewdidload() { super.viewdidload() self.map.delegate = self manager.requestalwaysauthorization() let latdelt = 0.05 let londelt = 0.05 let span:mkcoordinatespan = mkcoordinatespan(latitudedelta: latdelt, longitudedelta: londelt) let region:mkcoordinateregion = mkcoordinateregion(center: cllocationcoordinate2dmake(location.coordinate.latitude, location.coordinate.longitude), span: span) map.setregion(region, animated: true) map.maptype = mkmaptype.satellite locmark = mkplacemark(coordinate: cllocationcoordinate2dmake(location.coordinate.latitude, location.coordinate.longitude), addressdictionary: nil) destmark = mkplacemark(coordinate: cllocationcoordinate2dmake(next.coordinate.latitude, next.coordinate.longitude), addressdictionary: nil) source = mkmapitem(placemark: locmark) destination = mkmapitem(placemark: destmark) request.setsource(source) request.setdestination(destination) request.transporttype = mkdirectionstransporttype.automobile request.requestsalternateroutes = true let directions = mkdirections(request: request) directions.calculatedirectionswithcompletionhandler { (response:mkdirectionsresponse?, error:nserror?) -> void in if error == nil { self.directionsresponse = response! self.route = self.directionsresponse.routes[0] as! mkroute self.map.addoverlay(self.route.polyline) } else { println(error) } } func mapview(mapview: mkmapview!, rendererforoverlay overlay: mkoverlay!) -> mkoverlayrenderer! { if overlay mkpolyline { var pr = mkpolylinerenderer(overlay: overlay) pr.strokecolor = uicolor.purplecolor() pr.linewidth = 5 return pr } return nil } }
i don't errors printed, not route shows on map @ all. i've been struggling 4 days now.
the problem rendererforoverlay
function nested inside viewdidload
:
override func viewdidload() { //your viewdidload code here... //rendererforoverlay declared here inside viewdidload... func mapview(mapview: mkmapview!, rendererforoverlay overlay: mkoverlay!) -> mkoverlayrenderer! { ... } } // <-- closing brace of viewdidload
because of this, rendererforoverlay
function hidden map view , not call resulting in no overlay displaying.
to fix this, move rendererforoverlay
function outside viewdidload
it's visible rest of class:
override func viewdidload() { //your viewdidload code here... } // <-- closing brace of viewdidload //put rendererforoverlay here outside viewdidload... func mapview(mapview: mkmapview!, rendererforoverlay overlay: mkoverlay!) -> mkoverlayrenderer! { ... }
Comments
Post a Comment