将Parse PFGeoPoint转换为地图上的注释

时间:2017-07-04 22:31:10

标签: swift parse-platform annotations maps

我正在尝试将Parse服务器中保存的PFGeopoint对象转换为注释,但我似乎无法弄清楚它并且我不知道什么是错的。 我试图应用这个问题的代码:Converting Parse GeoPoint into a CLLocation in Swift但我仍然无法理解。

这是我的代码:

import UIKit
import MapKit
import CoreLocation
import Parse

class MapVC: UIViewController, MKMapViewDelegate {


fileprivate let locationManager = CLLocationManager()
fileprivate var startedLoadingPOIs = false
fileprivate var places = [Place]()


var descLocation: PFGeoPoint = PFGeoPoint()
var mapHasCenteredOnce = false

    @IBOutlet weak var mapView: MKMapView!
    override func viewDidLoad() {
        super.viewDidLoad()

        mapView.delegate = self

        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
        locationManager.startUpdatingLocation()
        locationManager.requestWhenInUseAuthorization()
        mapView.userTrackingMode = MKUserTrackingMode.followWithHeading

    }

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

        var annotationView: MKAnnotationView?

        if annotation.isKind(of: MKUserLocation.self) {
            annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "User")
            annotationView?.image = UIImage(named: "loc.png")
        }

        return annotationView
    }






}




extension MapVC: CLLocationManagerDelegate {




    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {



        //1
        if locations.count > 0 {
            let location = locations.last!
            print("Accuracy: \(location.horizontalAccuracy)")

            //2
            if location.horizontalAccuracy < 100 {
                //3
                manager.stopUpdatingLocation()
                let span = MKCoordinateSpan(latitudeDelta: 0.014, longitudeDelta: 0.014)
                let region = MKCoordinateRegion(center: location.coordinate, span: span)
                mapView.region = region

                if !startedLoadingPOIs {
                    startedLoadingPOIs = true
                    let loader = PlacesLoader()
                    loader.loadPOIS(location: location, radius: 1000) { placesDict, error in
                        if let dict = placesDict {


                            let query = PFQuery(className: "posts")
                            if let latitude = (PFUser.current()?["location"] as AnyObject).latitude {
                                if let longitude = (PFUser.current()?["location"] as AnyObject).longitude {
                                    let geoPoint = PFGeoPoint(latitude: latitude, longitude: longitude)
                                    query.whereKey("postLocation", nearGeoPoint: geoPoint, withinKilometers: 1)
                                }
                            }
                            query.findObjectsInBackground { (objects: [PFObject]?, error: Error?) in
                                if error == nil {



                                    for object in objects! {
                                        let pfObject = object as PFObject

                                        let caption = pfObject["title"] as! String

                                        self.descLocation = object["postLocation"] as! PFGeoPoint

                                        let latitude: CLLocationDegrees = self.descLocation.latitude
                                        let longitude: CLLocationDegrees = self.descLocation.longitude
                                        let location = CLLocation(latitude: latitude, longitude: longitude)

                                        let place = Place(location: location, name: caption )
                                        self.places.append(place)
                                        let annotation = PlaceAnnotation(location: place.location!.coordinate, title: place.placeName)
                                        DispatchQueue.main.async {
                                        self.mapView.addAnnotation(annotation)
                                        }

                                    }
                                }
                            }



                        }
                    }
                }


}
}
}


}

1 个答案:

答案 0 :(得分:0)

我只有两条uanessacry线阻止了操作,移除了它们,它对我有用。

loader.loadPOIS(location:location,radius:1000){placesDict,error in                         如果让dict = placesDict {