I am trying to figure out a way to get all the latitude and longitude points from a MKPolyline drawn on a MKMapView on an iOS app.
I know the MKPolyline does not store latitude and longitude points, but I am looking for a way to build an array of lat and long the MKPolyline would touch on the map.
Anybody has specific possible solutions on this?
Thank you
EDIT:
After seeing the first response (thank you) I think I need to explain better what my code is doing:
- first I call "calculateDirectionsWithCompletionHandler" on a MKDirections object
- I get back MKRoute object which has a "polyline" property.
- then I call "addOverlay" on the mapview passing in the polyline from the MKRoute object
That's all.
So, I already have a polyline built for me. So I would like to get all the points found in the polyline somehow and map them to a lat and long…
Best Answer
To get coordinates of the polyline from an
MKRoute
, use thegetCoordinates:range:
method.That method is in the
MKMultiPoint
class whichMKPolyline
inherits from.That also means this works for any polyline -- whether it was created by you or by
MKDirections
.You allocate a C array big enough to hold the number of coordinates you want and specify the range (eg. all points starting from the 0th).
Example:
Depending on the route, be prepared for hundreds or thousands of coordinates.