Thoughts

Using the iOS MapKit framework

By Sina Karimi9th Jan 2012

Overview of MapKit

Recently I was lucky enough to be part of an iOS app project which implemented the MapKit framework (check it out here). The app itself relied heavily on MapKit as one of its dominant features so it was essential that this component worked flawlessly.

For those of you who have never worked with MapKit before, or who don't know what MapKit is - think of it as Apple's proprietary library to Google Maps. It's pretty stable and versatile, although it can cause headaches for first time users. I guess the point of this post is to provide you a very brief insight into our experience with this library.

What it does well

Although we weren't able to use every feature MapKit had to offer, we managed to go quite deeply into the few features that we actually needed.

Panning/Zooming

From a developers perspective nothing needs to be done for panning & zooming to work, although we did create a category which can assist you in getting the current zoom level value - this can be helpful when implementing the regionDidChange method to set a min/max zoom levels for the map.

Pin placement

As long as correct coordinates are provided then pin placement will be spot on. Just keep in mind when using custom pins that the correct offset & positioning must be set, otherwise the positioning of the pins will be slightly off.

Custom pins

Creating custom pins is as simple as sending a UIImage into a MKAnnotationView object.NOTE:Using a custom pin with MKPinAnnotation will not work, you must use MKAnnotationView.

Custom map overlays

One major requirement for this project was to have a custom map overlay to sit on top of Google maps. The best way to achieve this is to soruce a high resolution image of your overlay and split the image up into tiles to be placed over the map. The positioning of the overlay is then attached to each of the tiles. For further instructions on how to do this go tohttp://shawnsbits.com/blog/2010/12/23/mapkit-overlays-session-1-overlay-map/.

Challenge we faced

Routing

One of the challenges we faced was implementing a drawn route-line between two coordinates. Unfortunately MapKit does not have an inbuilt routing feature so a custom category was built in order for this to work.

The process to complete routing is as follows:

  1. Send the start & end coordinates to the Google maps API
  2. You will then receive a set of encoded points which will need to be decoded using a similar algorithm as below:
  3. Now you are able to create an MKPolyline based on the result of the decoded points. Please see attachment 'MKMapView+Routing' for the full implementation.
    Download MKMapviewRouting.zip

Quick tip

Using @2x images for custom pin images

This may not be apparent at first, but if you are drawing custom pin images using UIGraphics it is important to use a scale factor of 2 when you begin drawing the image. You may wonder why we used UIGraphics to draw the pins - our pins consisted of 2 images (foreground & background) so we had to merge both these images into a single UIImage so that we could set the image of our annotation view. If you are not merging images then UIGraphics is not needed and you will not be required to set a scale factor.