MobileMakers Week3 Day 2
MapKit, CLLocation & Custom Delegation
I didn’t get a change to finish yesterday’s challenge. I stayed up until 3:00 AM working on it. Definitely my brain is starting to feel it.
Today was an great day actually. I was running on 3 hours of sleep and going on my second large coffee by 9:00am. The day began with QA which has been the norm and basically you can ask anything you want, about any iOS topic whatsoever. This is great in cases you’re stuck or just can’t wrap your head around things.
After those ~20 minutes, we began our code review and I got to show my code! It was even better today because we had two teachers reviewing my code, as if one isn’t intimidating enough. Good thing for me, there were no bad comments and they even told me that they wouldn’t have done it any differently! Of course we were reviewing yesterday’s challenge so it still had issues. I then had a one on one chat with Fiaz, the second instructor that reviewed my code.
Fiaz gave me the push I was needing. I guess i’m starting to feel the pressure and reaching the borders of my “comfort zone”. That sweet spot where I have been for the last three plus years where I did not push myself to learn more things. The feeling is both great and strange. Great because I absolutely want to learn everything but strange because i’m sort of blaming myself for not pushing myself all this time.
Fiaz chatted with me for a while and I told him what my goals where.
1. End the cohort with job secured.
2. Get the highest level of certification MobileMakers has to offer.
Fiaz was straight up with me and told me that based on what he has seen, I am exceeding expectations and everything else i’m struggling with will come with time and experience. He also suggested that I should focus all my stregnths on my investment and take full advantage of the bootcamp.
He is absolutely right. I’m trying to do things so quickly that i’m losing focus on learning and rather focusing on being the first one to finish.
Anyway.. enough of me for today. Let’s move on to the code!
For the morning part of the class we jumped straight into MapKit. I was surprised that it was relatively simple to implement.
We also learned that MapKit is a powerful framework that makes it easy for users to display things on maps. MapKit can display items on a map by using coordinates. You can also narrow your map view by defining the maps region. Finally, we learned how to display pins on the map by passing a string that can represent anything around the world.
Towards the end of the morning session, we were also able to determine our current location in the map and show a pulsating indicator that represented our location. The last great touch was being able to display information to the user once a pin was tapped. These are called annotations.
You set annotations by setting up their coordinates and using the method of addAnnotation:. Just like adding annotations, you can modify the pins color or visual representation. You could give the pin an image and display all the pins with an image. You do all this by importing MKMapViewDelegate.
On to the code.
In our storyboard, we get our view controller and drag in a mapview. We constraint it to the all four edges. We then add an outlet to our view controller’s implementation file.
We proceed to import the webKit frame work. We set our MKMapViewDelegate and we proceed to add two properties. One MKPointAnnotation and one CLLocationManager.
In our viewDidLoad method we first create two double’s representing the latitude and longitude. We proceeded to create an instance of our MKPointAnnotiation. MKPointAnnotation has a coordinate property and call that property and we use CLLocationCoordinate2DMake which takes in two parameters, one latitude and one logitude. We use that method to create a location for the pin. The final step is to use the addAnnotation method to pass the created MKPointAnnotatio.
We are going to use three methods in our view controller. One to update the annotation view we just created and the second that detects the touch of the annotation view.
The first method that we implemented was viewForAnnotation in which we updated the pin to return MobileMaker’s logo. In this method, we created an instance of MKPinAnnotationView. The PinAnnotationView has an image property that we used to assign an image to our pin. We then used a property that when set to YES, it allows us to display the annotation view. Finally we use a that enables us to add an accessoryView to the annotation view.
The second method that we implemented was calloutAccessoryControlTapped. In this method we created an instance of CLLocationCoordinate2D and passed the coordinate. We then create a coordinate span that basically takes a latitudeDelta and a longuteDelta. This gives you the radius as to how far you want to show the pins.
Finally, we have to implement a custom instance method that will take an NSString as a parameter so that we can display multiple pins on the map by passing a string such as a city or a popular landmark, etc. To do this we need to implement an instance of CLGeocoder and that property has a class method of geocodeAddressString. We use that method and pass in our address string. We then proceed to add the annotation to our pin that will show up when we tap a pin.
Here is the link in GitHub: MobileMapper
Today’s challenge picks up where we left off in the morning. We had to create an app that displayed all the bus stops in Chicago area. We needed display the bus stations with pins and also show the list of bus stops in a tableview. Basically the app had a segment control and you would alternate from one to the other.
The app also needed to implement a way to tap on the pin accessory view or tableview cell and take you to the detail view controller that would show more information on that particular bus stop.
Chicago also has two unique types of bus stops, one called Metra and another one called Pace. Our map needed a away to display those pins in different colors.
Here’s the final outcome:
I pretty much followed the same process as before and then used the array of pins and passed them onto my tableview. I did run into a snag though. I created a subclass of NSObject to create my model. Now this enabled me to download the data.. but could not display it. After about 30 minutes of looking and reading, I finally found the solution.. all in one single place. Apple docs. They gave me a step by step instruction on how to display the pins on my map! Finally I am getting better at finding things in Apple docs. When just a week ago it felt like reading Chinese.. it is now becoming maybe French. The solution was simple, I had to subclass MKPointAnnotation instead of NSObject. I still can’t understand why NSObject didn’t work since all classes inherit from NSObject.. but that will be a time for later.
In my detail view controller, I just passed the model via a segue and used my model’s properties to access information about the stop.
Here is the link in GitHub: GetOnTheBus