The definitive iPhone MapKit polyline example project

Polylines in MapKitPolylines in MapKit. Seems like such an obvious thing to have, yet completely not provided for in Apple’s default SDK. Why? Who knows; perhaps they don’t want to encourage people to write applications which violate Google Map’s terms of use (which forbids writing applications which provide real time navigation services).

That certainly hasn’t stopped people from trying to build their own homebrew polyline classes to plug into MapKit. In particular, Craig Spitzkoff wrote up a document describing a method of displaying a polyline on the map using an annotation, albeit not updated in real time (the polyline would disappear when the user is performing a zoom action on the map). Nicolas Neubauer later expanded upon Craig’s idea with the idea of hooking into the annotation view’s center property (called by the map kit view) to get notification to resize the polyline appropriately on zoom and move events. Unfortunately his description has a few errors (you need to hook into MKAnnotationView, not MKAnnotation), but the general idea was correct and was a rather elegant hack around the limitations of MapKit.

In any event, it so happened that I needed something to display a polyline for the Transit to Go project and was a bit perplexed about how to go about it. To resolve the confusion around how to do this properly once and for all, I decided to code up a quick sample implementation of a zoomable, transparent polyline on a MapKit map. The implementation is extremely bare bones and unoptimized but gets the tricky bits out of the way so you can concentrate on modifying it and improving it for your application’s needs. I decided that creating a general framework around this idea was kind of overkill, so decided to leave it to people to copy the relevant source files into their applications. I’ve placed the work under an extremely liberal license in the hopes that it will spread far and wide. Just remember to respect Google Map’s terms of use(read: no real-time navigation) in your app.

Go ahead and checkout the repository on github. You know you want to.