iOS15 polyline does not show on mapView

550 views Asked by At

I tried to add a polyline from the user's location to destination by using the following code, I am sure that I have conformed to the delegate and ensure that the user is in authorizedAlway mode in the authorization status. However, the console generated this error message saying "[UserSession] maps short session requested but session sharing is not enabled" I can not find anything related on how to solve this error.

    func generatePolyLine(toDestination destination: MKMapItem) {
    
    let request = MKDirections.Request()
    //start from the user's current location to find the ride
    request.source = MKMapItem.forCurrentLocation()
    request.destination = destination
    request.transportType = .automobile

    let directionRequest = MKDirections(request: request)

    directionRequest.calculate { response, error in
        if let error = error {
            print("Error calculating direction request \(error)")
        }
        guard let response = response else { return }
        self.route = response.routes.first
        guard let polyLine = self.route?.polyline else { return }
        self.mapView.addOverlay(polyLine, level: .aboveRoads)
        
    }
}
1

There are 1 answers

1
Magnas On

Did you add the delegate method to specify the renderer? Something like:

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
    if let polyline = overlay as? MKPolyline {
        let renderer = MKPolylineRenderer(polyline: polyline)
        renderer.lineWidth = 3.0
        renderer.alpha = 0.5
        renderer.strokeColor = UIColor.blue
        
        return renderer
    }
    if let circle = overlay as? MKCircle {
        let renderer = MKCircleRenderer(circle: circle)
        renderer.lineWidth = 3.0
        renderer.alpha = 0.5
        renderer.strokeColor = UIColor.blue
        
        return renderer
    }
    return MKCircleRenderer()
}