Should I use MKAnnotation, MKAnnotationView or MKPinAnnotation?

落花浮王杯 提交于 2019-11-29 04:48:58

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!