Enrique Espinosa
Enrique Espinosa

Reputation: 195

Why does my Polyline only draw a point and not draw a line?

enter image description hereI have a problem drawing a Polyline on my MapKit map, I am supposed to be trying to draw a line where the user is going, the problem is that when I move the user's position it only draws a point in the new position but does not trace the complete line

I have already tried to paint the line with a starting point and a starting point, but it does not work for me, it is assumed that if the user walks his position is updated and he should go drawing the line

This is my method didUpdateLocations:

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

        if let location = locations.last{
            self.currentLocation = location
            mapKit.centerCoordinate = location.coordinate
            //Validamos si el usuario quiere recordar su ruta
            if SettingsViewController.recordRutaOn{
             points.append(location)
             print("Estamos registrando la ruta del usuario")

            }

            //Prueba para pintar la ruta
            print((locations.last?.coordinate.latitude)!,(locations.last?.coordinate.longitude)!)
            var rutaPoly = CLLocationCoordinate2D(latitude: (locations.last?.coordinate.latitude)!, longitude: (locations.last?.coordinate.longitude)!)
            let polyline = MKPolyline(coordinates: &rutaPoly, count: locations.count)
            mapKit.add(polyline)
                if self.currentLocation == nil{
                self.currentAnnotation = MKPointAnnotation()
                self.currentAnnotation?.coordinate = location.coordinate
                self.mapKit.addAnnotation(self.currentAnnotation!)
            }
        }
    }

And this is my renderFor method:

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {

        if overlay is MKPolyline{
            let polyline = overlay
            let polyLineRender = MKPolylineRenderer(overlay: polyline)
            polyLineRender.strokeColor = UIColor.blue
            polyLineRender.lineWidth = 2.0            

            return polyLineRender
        }
        return MKPolylineRenderer()
    }

I don't understand what I'm doing wrong that only one point is drawn

Upvotes: 1

Views: 485

Answers (1)

Rob
Rob

Reputation: 437532

You have defined rutaPoly as a single CLLocationCoordinate2D. Thus, when you create the MKPolyline from that, there is only one coordinate and thus the rendered polyline will be a point. Worse, the count parameter you’re supplying to MKPolyline has nothing to do with the first parameter and you can get some undefined behaviors that way.

You want to keep an array of coordinates and append your new coordinate to that. Something like:

var coordinates: [CLLocationCoordinate2D] = []
var currentPolyline: MKPolyline?

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    guard let location = locations.last else { return }

    coordinates.append(location.coordinate)
    let polyline = MKPolyline(coordinates: coordinates, count: coordinates.count)
    mapView.addOverlay(polyline)
    if let currentPolyline = currentPolyline {
        mapView.removeOverlay(currentPolyline)
    }
    currentPolyline = polyline
}

Or, alternatively, you can add each new location update as a separate polyline:

var previousCoordinate: CLLocationCoordinate2D?

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    guard let location = locations.last else { return }

    if let previousCoordinate = previousCoordinate {
        let coordinates = [previousCoordinate, location.coordinate]
        let polyline = MKPolyline(coordinates: coordinates, count: coordinates.count)
        mapView.addOverlay(polyline)
    }
    previousCoordinate = location.coordinate
}

But whatever you do, make sure that the MKPolyline initializer is supplied an array of CLLocationCoordinate2D and its respective count.

Upvotes: 1

Related Questions