user1904273
user1904273

Reputation: 4764

Convert CLLocation to MKMapItem in Swift

I have a CLLocation for the User's location that I have retrieved from Core Location's locationManager. In order to calculate the distance to a map item, I would like to convert the CLLocation into a MKMapItem.

I gather you can get the coordinates of the CLLocation, make an MKPlacemark from them and finally make an MKMapItem from the MKPlacemark doing something like the following:

let currentLocation:CLLocation = locationManager.location
var coord : CLLocationCoordinate2D = currentLocation.coordinate
let myPlacemark = MKPlacemark(coordinate: coord)
let myMapItem = MKMapItem(placemark: myPlacemark)

This seems rather lengthy for this task and I am wondering, if there is no more direct way to do this?

Thanks in advance for any suggestions.

Upvotes: 1

Views: 1295

Answers (1)

Smartcat
Smartcat

Reputation: 2882

Since it's the user's current location you're trying to create an MKMapItem of, you can just do this:

let myMapItem = MKMapItem.forCurrentLocation()

That saves you the trouble of obtaining user's location, too! :)

Upvotes: 4

Related Questions