为什么MKPointAnnotation不会出现在地图上? [英] Why won't MKPointAnnotation show up on map?

查看:130
本文介绍了为什么MKPointAnnotation不会出现在地图上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个地图设置来显示带有此代码的注释:

I have a map setup to show an annotation with this code:

var newAnnotation = MKPointAnnotation()

    if newPostJustPosted {
        var newPostCoordinate = CLLocationCoordinate2DMake(userPosts.last!.postLatitude, userPosts.last!.postLongitude)
        newAnnotation.coordinate = newPostCoordinate
        newAnnotation.title = userPosts.last!.postTitle
        mainMapView.addAnnotation(newAnnotation)
        newPostJustPosted = false
        println("New post was just posted")
    }

这没有问题,并且注释显示出来。在此之后,我尝试通过以下代码放置一系列注释:

This runs no problem, and the annotation shows up. After this, I tried to place an array of annotations via this code:

if userPosts.count > 0 {
        for eachPost in 0...userPosts.count - 1 {
            var currentPost = userPosts[eachPost]
            newAnnotation.coordinate = CLLocationCoordinate2DMake(currentPost.postLatitude, currentPost.postLongitude)
            println("Latitude is: " + "\(currentPost.postLatitude)")
            newAnnotation.title = currentPost.postTitle
            mainMapView.addAnnotation(newAnnotation)
            println("This Ran")
        }
    }

根据控制台,纬度和经度是正确的,代码运行的次数正确,但地图上没有注释。我现在花了好几个小时查看这段代码,如果它很简单,我会感到很愚蠢。无论哪种方式,任何帮助将不胜感激!谢谢!

According to the console, the Latitude and Longitude are correct, and the code is running the correct number of times, but no annotations show up on the map. I've now spent hours looking over this code and if it's something simple I'm going to feel so dumb. Either way, any help would be appreciated! Thanks!

推荐答案

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.

这篇关于为什么MKPointAnnotation不会出现在地图上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆