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.
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-definedMKPointAnnotation
class (which implements theMKAnnotation
interface) for your annotation data model objects instead of creating a custom class if all you need istitle
,subtitle
, andcoordinate
.MKAnnotationView
is how the annotation's view should appear and is set in the map view'sviewForAnnotation
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 singleMKAnnotationView
instance might be used for multiple annotation objects that implementMKAnnotation
(assuming they are not all on the screen at once).So you create an
MKAnnotation
-based object and pass that in theaddAnnotation
(ie.AddAnnotationObject
) call. Then in theviewForAnnotation
delegate method, you create and return an instance of anMKAnnotationView
-based object."MKPinAnnotation" (actually
MKPinAnnotationView
) is a pre-defined subclass ofMKAnnotationView
which provides a default pin image. You can return an instance ofMKPinAnnotationView
in theviewForAnnotation
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 aUIButton
of typeUIButtonTypeDetailDisclosure
and set it as the annotation view'srightCalloutAccessoryView
.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.