-1

Cannot convert value of type '[Dictionary]' to expected argument type 'UnsafePointer'

Is the error I'm getting when trying to insert coordinates into my array.

Here is my code:

var locations: [CLLocationCoordinate2D] = []

    //Updating location + polylines
    @objc func update()
    {
        Annotationtimer = Timer.scheduledTimer(withTimeInterval: 10, repeats: true, block: { (timerr) in

            let currentLat = self.locationManager.location?.coordinate.latitude
            let currentLong = self.locationManager.location?.coordinate.longitude


            let location = [(currentLat!), (currentLong!)]


            self.locations.append(location) //HERE IS THE ERROR
            print(self.locations)


     //Draw polyline on the map
     let aPolyLine = MKPolyline(coordinates: locations, count: locations.count)

     //Adding polyline to mapview
     self.mapView.addOverlay(aPolyLine)


        })
    }

I'm trying to insert current location into an array, and when I press a button, I wanna draw polylines from start, through all coordinates.

What am I doing wrong? How can I insert the coordinates correctly? I've read other threads about this, but none of them is actually working. So I'd to create a new one.

Why dislike? At least you can tell me the reason for it.

  • Simply replace 'let location = ...' with 'let location = CLLocationCoordinate2D.init(latitude: currentLat!, longitude: currentLong!)' . Your issue is that you were adding an array of CGFloats instead of adding a cllocationcoordinate – marc Mar 01 '19 at 14:50

1 Answers1

1

currentLat and currentLong variables are String, and array type is CLLocationCoordinate2D.

You don't need to extract currentLat and currentLong, it's better to do like this : self.locations.append(self.locationManager.location?.coordinate)

Also, my advice - use guard to save app from crashing in situation, where self.locationManager.location == nil.

So, here is an example:

guard let currentLocation = self.locationManager.location else { return } // show error or something else self.locations.append(currentLocation)