maps - Swift MapKit not showing directions to pin -
i have written code drop pin , show directions on mapkitview , reason pin drops directions not show on map. welcome, thank you.
import uikit import mapkit import corelocation class viewcontroller: uiviewcontroller, cllocationmanagerdelegate, mkmapviewdelegate { @iboutlet weak var mapview: mkmapview! let locationmanager = cllocationmanager() var movedtouserlocation = false func clean() { mapview.removeannotations(mapview.annotations) mapview.removeoverlays(mapview.overlays) } func locationmanager(_ manager: cllocationmanager, didchangeauthorization status: clauthorizationstatus) { switch status { case .denied, .restricted: print("disable parental controles") case .notdetermined: manager.requestwheninuseauthorization() default: manager.startupdatinglocation() } } func mapview(_ mapview: mkmapview, didupdate userlocation: mkuserlocation) { if !movedtouserlocation { mapview.region.center = mapview.userlocation.coordinate movedtouserlocation = true } } func adddirections(coor: cllocationcoordinate2d) { let directionsrequest = mkdirectionsrequest() directionsrequest.source = mkmapitem(placemark: mkplacemark(coordinate: mapview.userlocation.coordinate)) directionsrequest.destination = mkmapitem(placemark: mkplacemark(coordinate: coor)) directionsrequest.requestsalternateroutes = false directionsrequest.transporttype = .any let directions = mkdirections(request: directionsrequest) directions.calculate { response, error in if let res = response { if let route = res.routes.first { self.mapview.add(route.polyline) self.mapview.region.center = coor } } else { print(error) } } } @ibaction func b1(_ sender: any) { let lat = 30.2817 let lon = -86.0188 self.clean() let coord = cllocationcoordinate2dmake(cllocationdegrees(lat), cllocationdegrees(lon)) let annotationview: mkpinannotationview! let annotationpoint = mkpointannotation() annotationpoint.coordinate = coord annotationpoint.title = "pizza sea" annotationview = mkpinannotationview(annotation: annotationpoint, reuseidentifier: "annotation") mapview.addannotation(annotationview.annotation!) adddirections(coor: coord) } @ibaction func b2(_ sender: any) { let lat = 30.2844 let lon = -86.0272 self.clean() let coord = cllocationcoordinate2dmake(cllocationdegrees(lat), cllocationdegrees(lon)) let annotationview: mkpinannotationview! let annotationpoint = mkpointannotation() annotationpoint.coordinate = coord annotationpoint.title = "george's" annotationview = mkpinannotationview(annotation: annotationpoint, reuseidentifier: "annotation") mapview.addannotation(annotationview.annotation!) adddirections(coor: coord) } @ibaction func b3(_ sender: any) { let lat = 30.2815 let lon = -86.0191 self.clean() let coord = cllocationcoordinate2dmake(cllocationdegrees(lat), cllocationdegrees(lon)) let annotationview: mkpinannotationview! let annotationpoint = mkpointannotation() annotationpoint.coordinate = coord annotationpoint.title = "ticheli's pizza" annotationview = mkpinannotationview(annotation: annotationpoint, reuseidentifier: "annotation") mapview.addannotation(annotationview.annotation!) adddirections(coor: coord) } @ibaction func b4(_ sender: any) { let lat = 30.2794074 let lon = -86.0816672 self.clean() let coord = cllocationcoordinate2dmake(cllocationdegrees(lat), cllocationdegrees(lon)) let annotationview: mkpinannotationview! let annotationpoint = mkpointannotation() annotationpoint.coordinate = coord annotationpoint.title = "big bad breakfast" annotationview = mkpinannotationview(annotation: annotationpoint, reuseidentifier: "annotation") mapview.addannotation(annotationview.annotation!) adddirections(coor: coord) } @ibaction func b5(_ sender: any) { let lat = 30.3129063 let lon = -86.1119704 self.clean() let coord = cllocationcoordinate2dmake(cllocationdegrees(lat), cllocationdegrees(lon)) let annotationview: mkpinannotationview! let annotationpoint = mkpointannotation() annotationpoint.coordinate = coord annotationpoint.title = "cafe thirty-a" annotationview = mkpinannotationview(annotation: annotationpoint, reuseidentifier: "annotation") mapview.addannotation(annotationview.annotation!) adddirections(coor: coord) } func mapview(_ mapview: mkmapview, rendererfor overlay: mkoverlay) -> mkpolylinerenderer { let renderer = mkpolylinerenderer(overlay: overlay as! mkpolyline) renderer.strokecolor = .yellow renderer.linewidth = 5.0 return renderer } @objc func dropannotation(gesturerecogniser: uigesturerecognizer) { if gesturerecogniser.state == .began { let holdlocation = gesturerecogniser.location(in: mapview) let coord = mapview.convert(holdlocation, tocoordinatefrom: mapview) let annotationview: mkpinannotationview! let annotationpoint = mkpointannotation() self.clean() annotationpoint.coordinate = coord annotationpoint.title = "\(coord.latitude), \(coord.longitude)" annotationview = mkpinannotationview(annotation: annotationpoint, reuseidentifier: "annotation 2") mapview.addannotation(annotationview.annotation!) adddirections(coor: coord) } } override func viewdidload() { super.viewdidload() mapview.delegate = self locationmanager.delegate = self locationmanager.desiredaccuracy = kcllocationaccuracybest let span = mkcoordinatespan(latitudedelta: mapview.region.span.latitudedelta/200, longitudedelta: mapview.region.span.longitudedelta/200) let region = mkcoordinateregion(center: mapview.region.center, span: span) let droppin = uilongpressgesturerecognizer(target: self, action: #selector(self.dropannotation(gesturerecogniser:))) droppin.minimumpressduration = cftimeinterval(1.0) mapview.addgesturerecognizer(droppin) mapview.setregion(region, animated: true) } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } }
Comments
Post a Comment