我应该使用 MKAnnotation、MKAnnotationView 还是 MKPinAnnotation?
我正在调用一个网络服务并在 MapView 上绘制一些对象。
我不明白的是我应该使用 MKAnnotationPoint 还是 MkAnnotationView?我不知道。当我使用 MKAnnotation 时,图钉会显示在地图上,如果我单击它们,它们会显示标题和副标题。
如果我使用 MKAnnotationView,引脚会显示,但当我单击它们时,什么也不会发生。
我还尝试添加正确的按钮标注/V 形按钮,但也无法弄清楚。
这是我的代码的示例。
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);
因此,如果我执行上述操作 - 引脚会显示,但我无法单击其中任何一个。
如果我只是使用:
mapView.AddAnotationObject(mk); //not using the MkAnnotationView
那么它们都会显示出来并且可以点击。不过,我不确定如何向它们添加正确的标注按钮。这是问题的第二部分。
谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我没有使用过 MonoTouch,但我相信底层 SDK 用法与 iOS 相同。
MKAnnotation
是注释数据模型所基于的协议/接口。如果您只需要,您还可以为注释数据模型对象使用预定义的
、MKPointAnnotation
类(它实现MKAnnotation
接口),而不是创建自定义类>标题副标题
和坐标
。MKAnnotationView
是注释视图的显示方式,并在地图视图的viewForAnnotation
委托方法中设置(不是内联创建)。注释视图可以在具有相同外观(例如图钉图像)的多个注释对象之间重复使用。因此,理论上,单个MKAnnotationView
实例可用于实现MKAnnotation
的多个注释对象(假设它们并非同时全部显示在屏幕上)。因此,您创建一个基于
MKAnnotation
的对象,并将其传递到addAnnotation
(即AddAnnotationObject
)调用中。然后,在viewForAnnotation
委托方法中,创建并返回基于MKAnnotationView
的对象的实例。“MKPinAnnotation”(实际上是
MKPinAnnotationView
)是MKAnnotationView
的预定义子类,它提供默认的图钉图像。您可以在viewForAnnotation
委托方法中返回MKPinAnnotationView
的实例,而不是设计自定义视图。您将在 viewForAnnotation 委托方法中创建正确的标注附件按钮。您创建一个
UIButtonTypeDetailDisclosure
类型的UIButton
并将其设置为注释视图的rightCalloutAccessoryView
。按钮按下将在地图视图的 calloutAccessoryControlTapped 委托方法中处理,该方法提供对注释视图和标注所在注释的引用。
给出的方法名称是 iOS 中的名称,但 MonoTouch 中的名称应该是相似。
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.