简体   繁体   中英

Converting Parse GeoPoint into a CLLocation in Swift

I am working in Swift.

In my Parse backend I have a key called locations which has a number of geoPoint values of which are latitude and longitude points. I want to query/fetch all these points and place them into an array so I can then use them as different annotations on a map.

I am having trouble querying the locations so that they can be used as a CLLocation for the map.

If anyone could help me do this it would be much appreciated.

You can create a variable as a PFGeoPoint and you can put your data from parse into this variable:

var descLocation: PFGeoPoint = PFGeoPoint()

var innerP1 = NSPredicate(format: "ObjectID = %@", objectID)
    var innerQ1:PFQuery = PFQuery(className: "Test", predicate: innerP1)

    var query = PFQuery.orQueryWithSubqueries([innerQ1])
    query.addAscendingOrder("createdAt")
    query.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]!, error: NSError!) -> Void in

        if error == nil {
            for object in objects {
                descLocation = object["GeoPoint"] as PFGeoPoint
            }
        } else {
            println("Error")
        }

    }

And in your class where you need the location, just add these line:

    var latitude: CLLocationDegrees = descLocation.latitude
    var longtitude: CLLocationDegrees = descLocation.longitude

    var location: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: latitude, longitude: longtitude)

So you can add annotation to your map using this code:

    @IBOutlet var map: MKMapView!
    var annotation = MKPointAnnotation()
    annotation.title = "Test"
    annotation.coordinate = location
    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