Jack Marchetti Jack Marchetti - 1 year ago 156
iOS Question

Should I use MKAnnotation, MKAnnotationView or MKPinAnnotation?

I'm calling a webservice and plotting some objects on a MapView.

What I'm failing to understand is should I use MKAnnotationPoint or MkAnnotationView? I'm not sure. When I use MKAnnotation, the pins show up on the map, if I click on them, they show me the title, and subtitle.

If I use MKAnnotationView, the pins show up but when I click on them, nothing happens.

I'm also trying to add the right button callout/chevron looking button but haven't been able to figure that out either.

Here's an example of my code.

MKPointAnnotation mk = new MKPointAnnotation();
MKAnnotationView mkView = new MKAnnotationView();
mk.Coordinate = coord;
mk.Title = tl.Name;
mk.Subtitle = DateTime.Now.ToShortDateString();
mkView.Annotation = mk;

So if I do the above - the pins show up, but I can't click on any of them.

If I just use:

mapView.AddAnotationObject(mk); //not using the MkAnnotationView

Then they all show up and are clickable. I'm not sure how to add the rightcalloutbutton to them yet though. So that's kinda a second part to the question.


Answer Source

I haven't used MonoTouch but the underlying SDK usage is the same as iOS I believe.

MKAnnotation is the protocol/interface to base your annotation data model on. You could also use the pre-defined MKPointAnnotation class (which implements the MKAnnotation interface) for your annotation data model objects instead of creating a custom class if all you need is title, subtitle, and coordinate.

MKAnnotationView is how the annotation's view should appear and is set in the map view's viewForAnnotation delegate method (not created inline). Annotation views can be re-used between multiple annotation objects that will have the same appearance (for example a pin image). So theoretically a single MKAnnotationView instance might be used for multiple annotation objects that implement MKAnnotation (assuming they are not all on the screen at once).

So you create an MKAnnotation-based object and pass that in the addAnnotation (ie. AddAnnotationObject) call. Then in the viewForAnnotation delegate method, you create and return an instance of an MKAnnotationView-based object.

"MKPinAnnotation" (actually MKPinAnnotationView) is a pre-defined subclass of MKAnnotationView which provides a default pin image. You can return an instance of MKPinAnnotationView in the viewForAnnotation delegate method instead of designing a custom view.

The place where you would create the right callout accessory button is in the viewForAnnotation delegate method. You create a UIButton of type UIButtonTypeDetailDisclosure and set it as the annotation view's rightCalloutAccessoryView.

The button press would be handled in the map view's calloutAccessoryControlTapped delegate method which provides a reference to the annotation view and annotation the callout is in.

The method names given are the ones in iOS but the names in MonoTouch should be similar.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download