【问题标题】:Swift Parse Query For PFGeoPoint Not Displaying Multiple AnnotationsPFGeoPoint 的 Swift 解析查询不显示多个注释
【发布时间】:2017-07-24 18:09:00
【问题描述】:

我在尝试将保存为 PFGeoPoints 的多个注释显示到我的地图时遇到问题。当我运行我的查询时,它工作正常,但是它只在地图上显示最近保存的注释,而我需要它来显示迄今为止保存的每个注释。

这是我的代码:

    let query = PFQuery(className: "location")

    query.findObjectsInBackground { (objects, error) in


        if let brandsLocation = objects {

            for object in brandsLocation {

                if let brandLocation = object as? PFObject {

                let annotation = MKPointAnnotation()


                let annotationPoint = object["geoPoint"] as! PFGeoPoint

                    self.annotation.coordinate = CLLocationCoordinate2DMake(annotationPoint.latitude, annotationPoint.longitude)

                    self.annotation.title = brandLocation["annotationTitle"] as? String

                    self.annotation.subtitle = brandLocation["annotationSubtitle"] as? String

                    self.map.addAnnotation(self.annotation)




            }

        }

    }
    }

非常感谢任何帮助,我知道这可能是一个小问题,导致并非我的所有注释都被查询。感谢您的帮助!!!!

【问题讨论】:

  • 您是否设置了断点以查看您在遍历对象时循环了多少次?
  • 不,我没见过我对断点不太熟悉。你能建议把它们放在哪里吗?我会在代码末尾的四个大括号之后思考。
  • 不,你会想要一个if let brandLocation...,看看它点击了多少次。如果它只命中一次,由于某种原因,您只命中了一个对象,这就是为什么您只得到一个注释。如果它多次命中,那么您将需要逐步查看您最终为注释属性设置的值。
  • 它肯定不止一次,实际上是我拥有的三个注释的三倍。但是,只有一个仍在显示
  • 注解是否有一些特定的东西会导致查询只产生一个注解?

标签: swift dictionary parse-platform annotations


【解决方案1】:

您在设置注释的属性并将其添加到地图时使用self.annotation...,因此let annotation = MKPointAnnotation() 属性将未使用。您必须声明一个单独的注释作为此处使用的类范围的一部分,而不是循环中的本地注释。

将来,展示您的整个代码,并可能单独指出相关的内容,这样可以让尚未使用代码库的人更容易发现此类问题。

【讨论】:

    【解决方案2】:

    我认为您在标题和副标题中引用了错误。这些值必须是唯一的。

    let query = PFQuery(className: "location")
    query.findObjectsInBackground { (objects, error) in
    
        if error != nil {
            print(error ?? "")
        }
    
        if objects != nil {
            for object in objects {
    
                let annotation = MKPointAnnotation()
                if let annotationPoint = object["geoPoint"] as? PFGeoPoint {
                    annotation.coordinate = CLLocationCoordinate2DMake(annotationPoint.latitude, annotationPoint.longitude)
                    annotation.title = object["annotationTitle"] as? String
                    annotation.subtitle = object["annotationSubtitle"] as? String
                    self.map.addAnnotation(annotation)
                }
            }
        }
    }
    

    【讨论】:

    • 您在功能上做了哪些改变?所有的更改似乎都是样式更改,除了您没有将对象转换为 PFObject,并且您将let annotationPoint... 更改为if let annotationPoint... 我错过了什么吗?被删除的 if let brandLocation... 不应该做任何事情,因为它嵌套在 for 循环中,所以我认为这不会是不可变值未设置的冲突。
    • 是的,这似乎导致了同样的问题
    • 同意,我的回答是错误的。我添加了我的代码以匹配 Jake T. 的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-12
    • 2012-08-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多