简体   繁体   中英

Swift Parse Query For PFGeoPoint Not Displaying Multiple Annotations

I'm having a problem when trying to display multiple annotations saved as PFGeoPoints to my map. When I run my query it's working fine, however it is only displaying the most recently saved annotation on the map, when instead I need it to display every annotation that has been saved to date.

Here's my code:

    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)




            }

        }

    }
    }

Any help is much appreciated, I know it's probably a small problem that is causing not all my annotations to be queried. Thanks for the help!!!!

You use self.annotation... when setting the properties of your annotation and adding it to the map, so the let annotation = MKPointAnnotation() property is going unused. You must have declared a separate annotation as part of the class scope that is being used here, rather than the local one within your loop.

In the future, show your whole code and maybe point out the relevant separately, it makes issues like this way easier to spot from someone who hasn't worked with the code base yet.

I think you are referencing wrong in the title and subtitle. These values must be unique.

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)
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM