Help needed now. I can draw lines with MKPolyline and MKPolylineView, but how to draw an arc or curve lines between two coordinates on the MKMapView? Great thanks.
The Final Solution
Finally I wrote myself these conclusions here to help who want it.
If you want to:
- just draw arc path on map
- just draw an image on map
- combine arc path and image as overlay on map
So the solutions are:
- There are MKPolyline and MKPolylineView to draw lines, MKPolygon and MKPolygonView to draw polygons, MKCircle and MKCircleView to draw circles. No one fit? Where is an arc? Oh, yes. Now the really solution: You create a custom class derived from MKOverlayPathView and override the -createPath method, in the -createPath you create an arc use CGContextAddArcToPoint or others functions you like, and associate the path you just create to the path property of MKOverlayPathView and the custom works are done. Then you add MKPolyline in the map(Yes! just MKPolyline!), Then in the -mapView:viewForOverlay: method you create the custom class take that polyline. Then, just run it, everything is runs as you wish. Magic? you can draw a MKPolyline as an arc!
- Just an image? Use MKAnnotationView. That's done! you can do it! I believe that!
- In my problem, I want to draw an arc with an image in the overlay. So I create a custom class conforms to the MKOverlay protocol, and a custom class derived from MKOverlayView to draw that overlay. Everything works fine but I can't draw any path on the map! I've set the lineWidth to 1,2,3,4... but it wasn't work! Ah..the solution is set the line width with MKRoadWidthAtZoomScale(zoomScale) function and you can see the path! That's done......
And some tips here:
- Use Core Graphic to draw paths, not MapKit coordinates nor UIView coordinates!
- In the CG functions just keep in mind the line width should be converted by this function:MKRoadWidthAtZoomScale(zoomScale)
- Hope to help
Reading the documentation, it seems that you can create an instance of
MKOverlayPathView
and assign an arbitraryCGPathRef
object to itspath
property. This path can contain both straight lines and arcs.I don't know (and the documentation doesn't mention) in what format the coordinates of the path should be (
MKMapPoint
orCLLocationCoordinate2D
come to mind) but you can probably find that out by experimenting a bit.You answered yourself in your comment already, but I'd like to point everyone at the excellent AIMapViewWrapper project on GitHub which includes sample code for plotting an arc path over a set of coordinates. In that project it's being used to draw the path a plane takes including a shadow path underneath it (and other stuff such as animating a plane along that path). Should come in handy for anyone taking a stab at this.