且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么MKPointAnnotation不会出现在地图上?

更新时间:2021-10-31 04:06:44

addAnnotation 的文档中,它说:


...地图视图保留指定的对象。

... The map view retains the specified object.

这意味着当你调用 addAnnotation 时,地图视图不仅为自己保留对该确切对象的引用,而且在它调用与之相关的委托方法时为你提供注释,例如 viewForAnnotation

That means when you call addAnnotation, the map view keeps a reference to that exact object not only for itself, but for you when it calls delegate methods related to an annotation such as viewForAnnotation.

地图视图需要让您知道完全它在谈论哪个注释。

The map view will need to let you know exactly which annotation it's talking about.

注释对象可以是完全自定义的类(只要它们符合 MKAnnotation 协议)而不是经历了传递给 addAnnotation 的对象副本的可能繁琐,耗时且可能不可能完成的任务,它只是保留对那些确切的引用对象。

The annotation objects can be completely custom classes (as long as they conform to the MKAnnotation protocol) and instead of going through the possibly tedious, time consuming, and maybe impossible task of making copies of the objects passed to addAnnotation, it simply keeps references to those exact objects.

在问题的代码中,循环中只有第一次调用 addAnnotation 会添加一个新的注释到地图。

In the code in the question, only the first call to addAnnotation in the loop adds a new annotation to the map.

以下调用最终只修改现有注释的坐标 title

The following calls end up only modifying that existing annotation's coordinate and title.

现在,如果 addAnnotation 至少可以运行它可能会很好控制台中的时间警告,例如具有此指针引用的注释已经存在 - 不再添加它,但它没有。

Now, it might be nice if addAnnotation at least gave a run-time warning in the console such as "Annotation with this pointer reference already exists -- not adding it again" but it doesn't.

相反,它只是忽略了请求再次添加。

Instead, it just ignores the request to add it again.

必须发生的是添加的符号位于数组的最后一个坐标处。

What must be happening is that the one annotation that is added is at the last coordinates in the array.



修复方法是为每个注释创建注释对象的新实例你想要添加。


The fix is to create a new instance of an annotation object for each annotation you want to add.