【问题标题】:Swift Parse Saving an Annotation from MapView as a PFGeoPointSwift Parse 将 MapView 中的注释保存为 PFGeoPoint
【发布时间】:2017-07-17 19:46:27
【问题描述】:

我有一个应用程序,我让用户在 MapView 上选择他们品牌所在的位置。当他们点击地图上的一个位置时,它会创建一个带有标题和副标题的注释。我现在要做的是在用户点击“下一步”按钮时将此注释转换为 PFGeoPoint,以便可以将其保存到 Parse 并稍后查询并显示在地图上供每个用户查看。

这是允许用户为其位置创建注释的代码:

//Annotations
func longpress(gestureRecognizer: UIGestureRecognizer){

    let touchPoint = gestureRecognizer.location(in: self.map)

    let coordinate = map.convert(touchPoint, toCoordinateFrom: self.map)

    let annotation = MKPointAnnotation()


   if isAnnotated == true {

    annotation.coordinate = coordinate

    annotation.title = PFUser.current()?.username

    annotation.subtitle = (PFUser.current()?.username)! + " is based here!"

    //removes annotation
    let allAnnotations = self.map.annotations
    self.map.removeAnnotations(allAnnotations)

    //adds annotation
    self.map.addAnnotation(annotation)

    print("REMOVED")

    isAnnotated = false

    } else {

    annotation.coordinate = coordinate

    annotation.title = PFUser.current()?.username

    annotation.subtitle = (PFUser.current()?.username)! + " is based here!"

    //removes annotation
    let allAnnotations = self.map.annotations
    self.map.removeAnnotations(allAnnotations)

    //adds annotation
    self.map.addAnnotation(annotation)

    print("ANNOTATION ADDED")

    isAnnotated = true

    }
}

我现在需要将注释保存为 PFGeoPoint,但是我不太确定如何去做。似乎 PFGeoPoint 想要 CLLocation 形式的位置,但我不知道如何将注释转换为该位置。如果需要,我可以提供到目前为止的代码以将其保存到 Parse,但它真的很混乱,因为我不知道如何将注释转换为可以保存到 Parse 的格式。任何帮助深表感谢。谢谢!

编辑 - 这是我的代码,我现在只需要一种方法将包含注释坐标的 geoPoint 变量保存为“下一个”函数下的 PFGeoPoint

    import UIKit
    import Parse
    import MapKit
    import CoreLocation

    class BrandLocation: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

var activityIndicator = UIActivityIndicatorView()

var isAnnotated = false

@IBOutlet var map: MKMapView!

var locationManager = CLLocationManager()

//QUESTIONABLE
var currentLoc: PFGeoPoint! = PFGeoPoint()

//QUESTIONABLE
var MapViewLocationManager:CLLocationManager! = CLLocationManager()

//ANNOTATION DECLARED
let annotation = MKPointAnnotation()

//Defines geoPoint as nil
var geoPoint : CLLocationCoordinate2D! = nil


func createAlert(title: String, message: String) {

    let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)

    alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) in

        alert.dismiss(animated: true, completion: nil)

    }))

    self.present(alert, animated: true, completion: nil)


}


//THIS IS WHERE PFGEOPOINT IS SAVED
@IBAction func next(_ sender: AnyObject) {

    activityIndicator = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
    activityIndicator.center = self.view.center
    activityIndicator.hidesWhenStopped = true
    activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
    view.addSubview(activityIndicator)
    activityIndicator.startAnimating()
    UIApplication.shared.beginIgnoringInteractionEvents()

    let brandLocation = PFObject(className: "location")


    //this is the main problem, I'm not sure how to take the coordinates I've defined in the geoPoint variable and now save them as a PFGeoPoint
    geoPoint = PFGeoPoint(location: )

    geoPoint["brandLocation"] = self.map.annotations

   // let geopoint: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)

    brandLocation["annotationTitle"] = self.map.annotations

    brandLocation["annotationSubtitle"] = self.map.annotations

    brandLocation.saveInBackground { (succes, error) -> Void in

        self.activityIndicator.stopAnimating()
        UIApplication.shared.endIgnoringInteractionEvents()

        if error != nil {

            self.createAlert(title: "Could not update profile", message: "There was a problem updating your profile")

            print(":(((")

        } else {

            self.createAlert(title: "Profile Updated", message: "Profile details successfully updated")

            print("MAPPED")

            self.performSegue(withIdentifier: "toUserFeed", sender: self)

        }

    }

    // self.performSegue(withIdentifier: "toUserFeed", sender: self)

}


//VIEW DID LOAD
override func viewDidLoad() {
    super.viewDidLoad()

    view.backgroundColor = UIColor.black

    self.navigationController?.isNavigationBarHidden = true

    self.tabBarController?.tabBar.isHidden = true

    navigationController?.navigationBar.barTintColor = UIColor.black

    locationManager.delegate = self

    locationManager.desiredAccuracy = kCLLocationAccuracyBest

    locationManager.requestWhenInUseAuthorization()

    locationManager.startUpdatingLocation()


    let uipgr = UITapGestureRecognizer(target: self, action: #selector(BrandLocation.longpress(gestureRecognizer:)))

    //uipgr.minimumPressDuration = 1

    //uipgr.numberOfTapsRequired = 1

    map.addGestureRecognizer(uipgr)

}

override func viewDidAppear(_ animated: Bool) {



}


//Setting up map & location zoom
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    let userLocation: CLLocation = locations[0]

    let latitude = userLocation.coordinate.latitude

    let longitude = userLocation.coordinate.longitude

    let latDelta: CLLocationDegrees = 0.05

    let lonDelta: CLLocationDegrees = 0.05

    let span = MKCoordinateSpan(latitudeDelta: latDelta, longitudeDelta: lonDelta)

    let location = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)

    let region = MKCoordinateRegion(center: location, span: span)

    self.map.setRegion(region, animated: true)

    locationManager.stopUpdatingLocation()


}

//Annotations
func longpress(gestureRecognizer: UIGestureRecognizer){

    let touchPoint = gestureRecognizer.location(in: self.map)

    let coordinate = map.convert(touchPoint, toCoordinateFrom: self.map)


    //DECLARED ABOVE
    //let annotation = MKPointAnnotation()

    //WORKING ON GEOPOINT
    // currentLoc = PFGeoPoint(location: MapViewLocationManager.location)

    //Declares and defines geoPoint

    let latitude = annotation.coordinate.latitude

    let longitude = annotation.coordinate.longitude

    self.geoPoint = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)


   if isAnnotated == true {

    annotation.coordinate = coordinate

    annotation.title = PFUser.current()?.username

    annotation.subtitle = (PFUser.current()?.username)! + " is based here!"

    //removes annotation
    let allAnnotations = self.map.annotations
    self.map.removeAnnotations(allAnnotations)

    //adds annotation
    self.map.addAnnotation(annotation)

    print("REMOVED")

    isAnnotated = false

    } else {

    annotation.coordinate = coordinate

    annotation.title = PFUser.current()?.username

    annotation.subtitle = (PFUser.current()?.username)! + " is based here!"

    //removes annotation
    let allAnnotations = self.map.annotations
    self.map.removeAnnotations(allAnnotations)

    //adds annotation
    self.map.addAnnotation(annotation)

    print("ANNOTATION ADDED")

    isAnnotated = true

    }
}

【问题讨论】:

    标签: swift parse-platform annotations


    【解决方案1】:

    使用注解坐标设置地理点。

    let latitude = annotation.coordinate.latitude
    let longitude = annotation.coordinate.longitude
    let geopoint: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
    

    为你保存功能

    for annotation in self.map.annotations {
        //get location of all annotation
        let latitude = annotation.coordinate.latitude
        let longitude = annotation.coordinate.longitude
        let geoPoint = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
    
        //save in Parse object
        let brandLocation = PFObject(className: "location")
        brandLocation["annotationTitle"] = annotation.annotationTitle
        brandLocation["annotationSubtitle"] = annotation.annotationSubtitle
        brandLocation["geoPoint"] = geoPoint
        brandLocation.saveInBackground { (succes, error) -> Void in 
    
        ....
    
        }
    }
    

    【讨论】:

    • 但是如何获取用户选择的注释坐标并将其转换为可以保存为PFGeoPoint进行解析的纬度和经度?
    • 好的,谢谢,看来它会工作。但是,我遇到的问题是我需要将 PFGeoPoint 保存在一个名为“next”的单独函数中,该函数与创建注释的函数不同。如何在注释功能之外调用地理点,以便在用户单击下一步时将其保存为 PFGeoPoint?
    • 这是一个不同的问题。您可以将地理点保存在(视图控制器)类变量中。只需定义 var geoPoint : CLLocationCoordinate2D! = nil 并通过 self.geoPoint 保存和调用它。如果您需要更多帮助,只需将您的代码发送给我。
    • 查看我上面在 EDIT 下添加的代码。我真的很感谢 Lengo 的持续回复!!!!!!
    • 查看“下一个”功能,如果你能给我一个关于如何保存 PFGeoPoint 的建议
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-22
    相关资源
    最近更新 更多