简体   繁体   English

在Swift中将Parse GeoPoint转换为CLLocation

[英]Converting Parse GeoPoint into a CLLocation in Swift

I am working in Swift. 我在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. 在我的Parse后端中,我有一个名为location的键,它具有许多geoPoint值,其中包括纬度和经度点。 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. 我在查询位置时遇到麻烦,因此它们可以用作地图的CLLocation。

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: 您可以创建一个变量作为PFGeoPoint,并将解析后的数据放入该变量中:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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