time.
time.

Reputation: 97

Polyline on map not visible

I try to add a Polyline to the map, but it is not visible.

locationManager.startUpdatingLocation()
guard let locValue: CLLocationCoordinate2D =
    locationManager.location?.coordinate else { return }
Map.setCenter(locValue, animated: true)

if !locations.contains(where: {$0.latitude == locValue.latitude && $0.longitude == locValue.longitude}) {
    locations.append(locValue)
    NSLog("Add: %f %f -> Count: %i", locValue.latitude, locValue.longitude, locations.count)

    let polyline = MKPolyline(coordinates: &locations, count: locations.count)
    Map.addOverlay(mapView(Map, rendererFor: polyline).overlay)
}

"My" mapView function:

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
    let polyLine = overlay
    let polyLineRenderer = MKPolylineRenderer(overlay: polyLine)
    polyLineRenderer.strokeColor = UIColor.red
    polyLineRenderer.lineWidth = 5.0
    return polyLineRenderer
}

Upvotes: 2

Views: 156

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100543

You can try

self.map.delegate = self

let polyline = MKPolyline(coordinates:locations, count: locations.count) 
self.map.addOverlay(routeLine)

Upvotes: 1

Related Questions