[英]GetViewForAnnotation is never called
I am creating a MapView where I want to display some custom Annotations. 我正在创建一个MapView,我想在其中显示一些自定义注释。
So I think usually what you do is add some IMKAnnotation
to the MKMapView
using the AddAnnotation
method. 因此,我认为通常您使用AddAnnotation
方法向MKMapView
添加一些IMKAnnotation
。 I make sure to invoke that on the main thread like: 我确保在主线程上调用它,例如:
new NSObject().InvokeOnMainThread(() => {
_mapView.AddAnnotation(myNewAnnotation);
});
After these I are added I do see that the MKMapView
now contains all the annotations I added in the Annotations
property when inspecting with the debugger. 添加完这些之后,我确实看到MKMapView
现在包含在使用调试器进行检查时在Annotations
属性中添加的所有注释。
However, the problem is that GetViewForAnnotation
is never invoked, no matter how I do it. 但是,问题是无论我如何执行,都不会调用GetViewForAnnotation
。
I've tried: 我试过了:
_mapView.GetViewForAnnotation += ViewForAnnotation;
private MKAnnotationView ViewForAnnotation(MKMapView mapView, IMKAnnotation annotation) {
// do stuff here
}
I've tried implementing my own delegate: 我尝试实现自己的委托:
public class MyMapViewDelegate : MKMapViewDelegate
{
public override MKAnnotationView GetViewForAnnotation(MKMapView mapView, IMKAnnotation annotation) {
// do stuff
}
}
_delegate = new MyMapViewDelegate();
_mapView.Delegate = _delegate;
I've tried using WeakDelegate
: 我试过使用WeakDelegate
:
public class MapView : ViewController, IMKMapViewDelegate
{
private MKMapView _mapView;
public override void ViewDidLoad() {
_mapView = new MKMapView();
_mapView.WeakDelegate = this;
}
[Export("mapView:viewForAnnotation:")]
public MKAnnotationView GetViewForAnnotation(MKMapView mapView, IMKAnnotation annotation) {
// do stuff
}
}
Nothing seems to trigger the GetViewForAnnotation
method. 似乎没有什么触发GetViewForAnnotation
方法。 Any ideas what I do wrong? 有什么想法我做错了吗?
EDIT: 编辑:
A bit more details what I have now. 我现在有更多的细节。
[Register("MapView")]
public class MapView : MvxViewController<MapViewModel>
{
private MKMapView _mapView;
private NMTAnnotationManager _annotationManager;
public override void ViewDidLoad()
{
base.ViewDidLoad();
_mapView = new MKMapView();
_mapView.GetViewForAnnotation += GetViewForAnnotation;
_annotationManager = new NMTAnnotationManager(_mapView);
var bindSet = this.CreateBindingSet<MapView, MapViewModel>();
bindSet.Bind(_annotationManager).For(a => a.ItemsSource).To(vm => vm.Locations).OneWay();
bindSet.Apply();
Add(_mapView);
View.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();
View.AddConstraints(
_mapView.AtTopOf(View),
_mapView.AtLeftOf(View),
_mapView.AtRightOf(View),
_mapView.AtBottomOf(View));
}
private MKAnnotationView GetViewForAnnotation(MKMapView mapview, IMKAnnotation annotation)
{
return null;
}
}
The NMTAnnotationManager
simply weak subscribes to the INotifyCollectionChanged
event the ObservableCollection
which is used as ItemsSource
in the binding. NMTAnnotationManager
只是弱订阅了ObservableCollection
的INotifyCollectionChanged
事件,该事件在绑定中用作ItemsSource
。 When the collection changes it simply adds and removes the Annotations from the MKMapView
, nothing magical happens here. 当集合发生更改时,它只需在MKMapView
添加和删除Annotations,这里就不会发生任何神奇的事情。 I have verified that it does indeed add, in this case 13, different IMKAnnotation
instances to the MKMapView
and they can be inspected in the Annotations
property of it. 我已经验证了它确实在这种情况下向MKMapView
添加了不同的IMKAnnotation
实例,并且可以在其Annotations
属性中对其进行检查,它们确实是13个。
So as @Philip suggested in his answer, GetViewForAnnotation
does get set before Annotations are added to the MapView. 因此,作为@Philip在他的回答表明, GetViewForAnnotation
注释添加到的MapView之前不会置位。 But if I put a breakpoint or some trace in the method it never gets hit. 但是,如果我在方法中放置一个断点或一些痕迹,它将永远不会受到打击。
The same code above, just with a simple MKMapViewDelegate
like so: 上面的相同代码,只是带有一个简单的MKMapViewDelegate
如下所示:
public class MyMapViewDelegate : MKMapViewDelegate
{
public override void MapLoaded(MKMapView mapView)
{
Mvx.Trace("MapLoaded");
}
public override MKAnnotationView GetViewForAnnotation(MKMapView mapView, IMKAnnotation annotation)
{
Mvx.Trace("GetViewForAnnotation");
return null;
}
}
Does not work either. 也不起作用。 Although, the MapLoaded
event gets hit for each time the map is rendered, but why does GetViewForAnnotation
not get hit? 虽然,每次渲染地图时MapLoaded
事件都会被命中,但是为什么GetViewForAnnotation
不被命中?
The way I'm doing it is: 我这样做的方式是:
In ViewDidLoad
I make sure I set this first: mapView.GetViewForAnnotation += GetViewForAnnotation;
确保在ViewDidLoad
进行设置: mapView.GetViewForAnnotation += GetViewForAnnotation;
I had problems with it never beeing called too, and I fixed it with making sure that I set the event for GetViewForAnnotation before I added some annotations. 我遇到了从来没有被蜂鸣的问题,并且在添加一些注释之前,请确保已为GetViewForAnnotation设置了事件,从而解决了该问题。
OK, so apparently the problem was PEBKAC. 好的,显然问题出在PEBKAC。
The annotation indeed got added to the MKMapView
. 注释确实已添加到MKMapView
。 However, none of them had their Coordinate
property set. 但是,它们都没有设置其Coordinate
属性。 So obviously the map did not know where to present them and never called GetViewForAnnotation
. 因此,显然地图不知道在哪里展示它们,并且从未调用GetViewForAnnotation
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.