Today’s workshop was all about implementing ‘Map Kit’ (an interface for embedding maps) into a application of our own. This I was excited for because I have seen it included in so many applications that I use on a day-to-day basis. I was also looking forward to seeing how it was possible to customise ‘pins’ on a map to create them at any specific point on the map. The aim of the workshop was to create a map that displayed the local coffee shops around Bournemouth’s town.
Initially I was quite surprised at how easy it was to load the map framework into the application, by simply going to the ‘capabilities’ tab and turning a little switch to on and changing the ‘Identity Inspector’ class to ‘MKMapView’ was enough for a fully interactive map to be loaded and used on xCode’s simulator.
The next stage was to create an ‘IBOutlet’ to the ‘View.Controller to import the ‘MapKit’ framework, then by setting the latitude and longitude into place I was able to get the map to centre on Bournemouth upon loading. This could be extended to having a list of different towns across the UK and then focussing on each one and displaying the coffee shops in each of them but because we only had an hour or so left, we decided to stay focused on Bournemouth.
After this a new Swift file was created for the annotation class. Then a class definition was created for ‘Coffee Shop’ and declared that it conforms to the ‘MKAnnotation’ protocol. However, as the class does not extend the ‘NSObject’ which is required, it produced an error. To fix this, the ‘Coffee Shop’ was turned into a subclass of the ‘NSObject’. Also a property of ‘coordinate’ is required to conform so that was also added along with ‘name’ and ‘comment’. An initialiser was also added so that instances could be created.
In the ‘View.Controller’, a property named ‘CoffeeShops’ was added and initialised with an array of instances. This was to display the three ‘pins’ onto the map. To add the name and comment to each one, the properties ‘title’ and ‘subtitle’ was taken from the ‘MKAnnotation’ protocol definition and then added to the ‘coffee shop’ class to get the ‘pins’ to display the information.
This is what I finished with at the end of the workshop: