5

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;             
mapView.AddAnnotationObject(mkView);

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.

Thanks.

Jack Marchetti
  • 15,536
  • 14
  • 81
  • 117

1 Answers1

13

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.

  • Thanks. I figured out how to add the calloutButton. However, I wonder if this is a MonoTouch issue. If I add MKMapView's to the mapView, nothing happens when I click on the annotation. If I simply add the MKAnnotation to the mapview it works. – Jack Marchetti Oct 29 '11 at 18:54
  • When you explicitly create a MKPinAnnotationView, the canShowCallout property defaults to NO--you have to set it to YES. If you don't create an annotation view explicitly (by not implementing the delegate) and just add the annotation, it creates a default annotation view with the canShowCallout set to YES. –  Oct 29 '11 at 19:14
  • hmmm, in my above example I set mkView.CanShowCallout = true; and still nothing happens when I click on the MKAnnotation - I must be doing something wrong. – Jack Marchetti Oct 30 '11 at 04:21
  • So basically what I'm doing is looping through and created a new MKAnnotation for each item returned from the web service. Should I specify an MKAnnotationView outside of that loop or do I assign the view for each annotation? – Jack Marchetti Oct 30 '11 at 04:46
  • You have to create the MKAnnotationView in the delegate method which will get called by the map view (not your code) when it needs to display that annotation. In iOS the delegate method (see MKMapViewDelegate protocol) is called viewForAnnotation. –  Oct 30 '11 at 14:12
  • that's where I'm confused. I'm not sure what a delegate method is. – Jack Marchetti Oct 30 '11 at 16:41
  • [This Xamarin site](http://docs.xamarin.com/ios) looks like a good place to learn how to use iOS MapKit on MonoTouch. [Here's an article on Events, Protocols, and Delegates](http://docs.xamarin.com/ios/tutorials/Events%2c_Protocols_and_Delegates). –  Oct 31 '11 at 01:49