2017-07-17 67 views
0

我有一個應用程序,我有用戶在他們的品牌所基於的MapView上選擇了一個位置。當他們點擊地圖上的某個位置時,它會創建一個帶有標題和副標題的註釋。我現在要做的就是在用戶點擊「下一步」按鈕時將此註釋轉換爲PFGeoPoint,以便將其保存到分析中,然後在地圖上查詢並顯示給每個用戶看。Swift解析從MapView保存一個註解作爲PFGeoPoint

這裏是允許用戶創建自己的位置註釋代碼:

//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下的「下一個」 FUNC

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 

    } 
} 

回答

0

使用標註座標設定一個GeoPoint 。

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 

    .... 

    } 
} 
+0

但我怎麼走註解座標是由用戶選擇,並將其轉換爲緯度和經度,可以保存爲PFGeoPoint解析? –

+0

更新了我的回答 – Lengo

+0

好吧,謝謝,似乎它會工作。然而,我遇到的問題是我需要將PFGeoPoint保存在一個名爲「next」的單獨函數中,該函數與創建註釋的函數不同。如何在註釋函數外調用geopoint,以便在用戶點擊下一步時將其保存爲PFGeoPoint? –