J'ai une application où l'utilisateur a choisi un emplacement sur MapView où sa marque est basée. Lorsqu'ils tapent un emplacement sur la carte, ils créent une annotation avec un titre et un sous-titre. Ce que j'essaie de faire maintenant est de convertir cette annotation en un PFGeoPoint lorsque l'utilisateur clique sur le bouton "suivant" pour qu'il puisse être sauvegardé dans Parse et plus tard interrogé et affiché sur une carte pour chaque utilisateur à voir.Swift Parse Sauvegarde d'une annotation de MapView en tant que PFGeoPoint
Voici le code qui permet à l'utilisateur de créer une annotation pour leur emplacement:
//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
}
}
Ce que je besoin maintenant d'enregistrer l'annotation comme PFGeoPoint, mais je ne suis pas trop sûr de savoir comment aller à propos de ça. Il semble que PFGeoPoint veut l'emplacement sous la forme de CLLocation, mais je ne sais pas comment convertir l'annotation à cela. Je peux fournir le code que j'ai jusqu'ici pour l'enregistrer dans Parse si nécessaire, mais il est vraiment brouillé car je ne sais pas comment obtenir l'annotation dans un format qui peut être sauvegardé dans Parse. Toute aide est très appréciée. Merci!
EDIT - Voici mon code, je maintenant juste besoin d'un moyen de sauver la variable Geopoint qui inclut l'annotation coordonnées comme PFGeoPoint sous la « prochaine » 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
}
}
mais comment puis-je prendre l'annotation coordonnée qui a été choisie par l'utilisateur et le convertir en une latitude et la longitude qui peut être enregistré en tant que PFGeoPoint pour analyser? –
mis à jour ma réponse – Lengo
D'accord merci ça semble que ça va marcher. Cependant, le problème que j'ai est que j'ai besoin de sauvegarder le PFGeoPoint dans une fonction séparée appelée "next" qui est différente de la fonction qui crée l'annotation. Comment puis-je appeler un géopoint en dehors de la fonction d'annotation pour pouvoir l'enregistrer en tant que PFGeoPoint lorsque l'utilisateur clique sur le bouton suivant? –