I'm trying to add a polyline for a route on MapView from location A to location B (which is the current user's GPS location), so the route/polyline would be able to follow from a set location A to wherever the user is current at (location B) on the fly.
Right now my overlay is not working. I looked at a few other SO threads on MKPolylineView, but when I tried implementing their code (which is below as well) the route/lines are still not showing. I'm new to iOS, so I'm still familiarizing with Swift/mapKit myself.
Here is what I have so far: (modified to show important parts)
class ViewController: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var setLocButton: UIButton!
@IBOutlet weak var mapView: MKMapView!
var locationManager: CLLocationManager = CLLocationManager()
var carLat = 36.136111, carLong = -80.279462
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [CLLocation]!) { //AnyObject->CLLocation
var latestLocation: CLLocation = locations[locations.count - 1]
//Displaying location A on mapView
let carCoords = CLLocationCoordinate2D(latitude: carLat, longitude: carLong)
let region = MKCoordinateRegion(center: carCoords, span: MKCoordinateSpan(latitudeDelta: 0.0035, longitudeDelta: 0.0035))
mapView.mapType = MKMapType.Hybrid
mapView.setRegion(region, animated: true)
//Attempting to display route information on mapView
mapView.showsUserLocation = true
var locations = [CLLocation(latitude: carLat, longitude: carLong), CLLocation(latitude: latestLocation.coordinate.latitude, longitude: latestLocation.coordinate.latitude)]
var coordinates = locations.map({(location: CLLocation) -> CLLocationCoordinate2D in return location.coordinate})
var polyline = MKPolyline(coordinates: &coordinates, count: locations.count)
self.mapView.addOverlay(polyline)
}
//rendererForOverlay
func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
if overlay is MKPolyline {
/* Does not get called */
print("rendererForOverlay")
var polylineRenderer = MKPolylineRenderer(overlay: overlay)
polylineRenderer.strokeColor = UIColor.blueColor()
polylineRenderer.lineWidth = 5
return polylineRenderer
}
return nil
}
I also found another example using MKDirections, which seems to be more ideal because it allows me to set the transport type (MKDirectionsTransportType.Walking). I'm having problem drawing the routes with those instructions too though.
Using the second set of instructions, here is what I got after resolving some errors Xcode alerted me of:
var route: MKRoute?
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [CLLocation]!) { //AnyObject->CLLocation
var latestLocation: CLLocation = locations[locations.count - 1] //AnyObject
/*
...
*/
//carLocation = Location A; currentLocation = location B
var directionsRequest = MKDirectionsRequest()
let carLocation = MKPlacemark(coordinate: carCoords, addressDictionary: nil)
var currentLocation = MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: coorLat, longitude: coorLong), addressDictionary: nil)
directionsRequest.source = MKMapItem(placemark: currentLocation)
directionsRequest.destination = MKMapItem(placemark: carLocation)
directionsRequest.transportType = MKDirectionsTransportType.Walking
var directions = MKDirections(request: directionsRequest)
directions.calculateDirectionsWithCompletionHandler {
(response, error) -> Void in
if error == nil {
self.route = response!.routes[0] as? MKRoute
self.mapView.addOverlay((self.route?.polyline)!)
}
}
}
func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
/* Does not get executed as well */
print("RendererForOverLay")
var myLineRenderer = MKPolylineRenderer(polyline: (route?.polyline)!)
myLineRenderer.strokeColor = UIColor.redColor()
myLineRenderer.lineWidth = 3
return myLineRenderer
}
Am I somehow linking the rendererForOverlay wrong, since it is not called in both instances?
Sofacoder is right, the polyline renderer works now!
I forgot to add myMap.delegate = self in my function, and also omitted MKMapViewDelegate the ViewController's declaration:
class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {