I'm new to iPhone development. I've been reading several questions on how to make a google maps annotation callout window accept line breaks. Every tutorial I've read requires me to fire the mapView:didSelectAnnotationView
method. But I have no idea how to trigger this. things I've tried include
- putting the method in my MapViewController.m file which extends UIViewController
- putting the method in a MapView.m file which extends MKMapView, then have my Mapview element in my storyboard reference it as the class to use
There's so much about xcode, objective c, and iphone development that I don't understand, so i can't tell where my problem lies.
At the moment, my map does plot my desired marker on the desired location. I just need to understand how to fire the mapView:didSelectAnnotationView
and mapView:viewForAnnotation
functions before I can start customizing the call out box.
Does anyone have step by step instructions on how to trigger these functions?
A bit of background
A few things to note:
You don't call
mapView:didSelectAnnotationView
. The MKMapView calls that function on it's delegate. In other words, when you set up an MKMapView, you tell it: "hey, listen, anytimme you need to tell me what's happening on the map, go tell this guy, he'll handle them for you". That "guy" is the delegate object, and it needs to implementmapView:didSelectAnnotationView
(that's also why its name "did select", ie, it already happened, as opposed to "select"). For a simple case, the delegate is often theUIViewController
that owns the MKMapView, which is what I'll describe below.That method will then get triggered when the user taps on one of your annotations. So that's a great spot to start customizing what should happen when they tap on an annotation view (updating a selection, for instance).
It's not, however, what you want if you want to customize what annotation to show, which is what it sounds like you're actually after. For that, there's a different method just a few paragraphs earlier on the same man page:
mapView:viewForAnnotation
. So substitute this method if you find thatmapView:didSelectAnnotationView
isn't what you were looking for.What you can do
If you got as far as a map with a marker, I'm guessing you have at least: * a view controller (extendeding from
UIViewController
, and * anMKMapView
that you've added to the view for that view controller, say namedmapView
The method you want to fire is defined as part of the
MKMapViewDelegate
protocol. The easiest way to get this wired is to:make your UIViewController the delegate for you MKMapView
viewDidLoad
, of yourMapViewController.m
you could domapview.delegate = self
, ORthen, define a method on your UIViewController called
mapView:didSelectAnnotationView
, declaring it just like the protocol does, in yourMapViewController.m
file:Good luck!
mapView:didSelectAnnotationView
is a delegate method of the map view, you can read about it here: MKMapViewDelegate Protocol ReferenceYou don't need to call it, the map view will call it "by it self" and send it to every view/view controller that registered as it's delegate.
What do you need to do
Basically you need to add the MKMapViewDelegate on your .h file, what will look something like this:
Then in the .m file, after you instantiate the map view you should add:
From this point and on your controller will be able to "receive messages" from the map view which are the methods that you can see on the MKMapViewDelegate reference I linked to.
So to implement the mapView:didSelectAnnotationView you need to add in your .m file
What is happening
What happens in the background is:
When a touch event take place it sends a "message" to all of it's delegates saying "Hey a user did Select Annotation View on this map, Do with it what ever you need". usually it looks like that:
Then every view/controller that assigned itself as a delegate and implemented the method will cal this method.
Good luck