1

I'd like to know about PFGeoPoint class. I read about it on Parse site but i got a question that doesn't seem to have answer. I need to save PFGeoPoint and to make it visibile to all user that use my application and i really don't know why. Can PFGeoPoint do it??

My app is quite simply: you can login (PFUser) and place a mark on a map where's a little fountain (for people who're looking for it). And i want to make all fountains marked (by all users) visibile to all other users unless i deleted it from parse.

@IBAction func changed(sender: UISegmentedControl) {

    switch rateFountain.selectedSegmentIndex
    {
        case 0:
            voto.text = "Good";
            break;
        case 1:
            voto.text = "Bad";
            break;
        default:
            voto.text = "Good";
            break;

    }
}

@IBAction func logout(sender: UIButton) {

    PFUser.logOut()
     print("Log Out Done")
     self.performSegueWithIdentifier("gotoLogin", sender: self)
}
@IBOutlet weak var map: MKMapView!

@IBAction func addFountain(sender: UIButton) {

    let ObFountain = PFObject(className: "Fountain")

    ObFountain["nome"] = nome.text
    ObFountain["voto"] = voto.text

    let fountain = MKPointAnnotation()
    fountain.coordinate = posizioneUtente
    fountain.title = nome.text
    fountain.subtitle = voto.text
    self.map.addAnnotation(fountain)


}
var MapViewLocationManager:CLLocationManager! = CLLocationManager()
var managerPosizione: CLLocationManager! //gestisce oggetti LocationManager
var posizioneUtente: CLLocationCoordinate2D!//coordinate



override func viewDidLoad() {
    super.viewDidLoad()

    managerPosizione = CLLocationManager() //inizializzo il locationManager che recupera la posizione utente
    managerPosizione.delegate = self
    managerPosizione.desiredAccuracy = kCLLocationAccuracyNearestTenMeters //setto l'accuratezza della nostra posizione con un errore di non oltre dieci metri
    managerPosizione.requestAlwaysAuthorization() // richiede da parte dell'utente utilizzatore l'attivazione delle funzioni di geolocalizzazione
    managerPosizione.startUpdatingLocation() // tiene traccia del cambiamento di posizione
        // Do any additional setup after loading the view.

}

func viewDidApperar(animated : Bool){
       }
//avvisa il delegate che le coordinate dell'utente sono state aggiornate
func mapView(mapView: MKMapView, didUpdateUserLocation userLocation: MKUserLocation) {
    posizioneUtente = userLocation.coordinate //salvo le coordinate dell'utente nella variabile

    print("posizione aggiornata - lat: \(userLocation.coordinate.latitude) long: \(userLocation.coordinate.longitude)")

    let span = MKCoordinateSpanMake(0.05, 0.05) // estensione dell'area da visualizzare

    let region = MKCoordinateRegion(center: posizioneUtente, span: span) // calcola le coordinate della regione

    mapView.setRegion(region, animated: true) //aggiorna la mapView
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

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

    //se l'annotation è la posizione dell'Utente allora esci dalla funzione e mostra il punto blu
    if annotation is MKUserLocation {
        return nil
    }

    //creo un id da associare ad ogni annotationView
    let reuseId = "punto"
    //se esistono troppi punti nella mappa, prende quello non visto e lo riutilizza nella porzione di mappa vista
    var puntoView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)

    //se non è stata ancora creata un'AnnotationView la crea
    if puntoView == nil {
        //creo un pin di tipo MKAnnotationView che rappresenta l'oggetto reale da inserire in mappa
        puntoView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        //cambio l'immagine standard del point annotation con una creata da me
        puntoView!.image = UIImage(named: "nella30.png")
        //sblocco la possibilità di cliccarlo per vedere i dettagli
        puntoView!.canShowCallout = true
    }
    else {
        //se esiste lo modifico con il nuovo point richiesto
        puntoView!.annotation = annotation
    }
    //restituisce un pointAnnotation nuovo o modificato
    return puntoView
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?){
    view.endEditing(true)
    super.touchesBegan(touches, withEvent: event)

}

My wish is to set the fountain when i press the button with my current Location and rated it with good or bad. And to show this to all users who use app. I'm pretty newbie to swift so you might be helping me with code directly. I'd appreciate a lot that

1 Answers1

0

PFGeoPoint is absolutely what you want. To give you some background information, PFGeoPoint is essentially a wrapper around the iOS CLLocation object which contains a latitude/longitude coordinate.

You have two situations you'll need to handle:

  1. Loading all nearby markers (fountains)
  2. Creating/saving a new marker

You should create another Parse class called Fountain or whatever you feel is most descriptive for your app. This class will contain the geopoint along with any other information you may need. When a user creates a new Fountain object, assign the location and other attributes (title, description, etc), and then save.

On the map itself, you will will want to query for Fountain objects and then use those to populate the map. You may also want to limit the query to nearby fountains for improved performance.

Since this is a pretty open-ended question, here's some examples for you to reference and welcome to StackOverflow!

Query a GeoPoint from Parse and add it to MapKit as MKAnnotation

MapKit Tutorial with Swift in iOS8

Russell
  • 3,099
  • 2
  • 14
  • 18
  • Thanks you Russel but I'm kinda new to Swift and trying to learn with my first app. Maybe i could post the code so you can help me better – Luca Dal Seno Nov 24 '15 at 15:37
  • Make sure to query Parse for the `Fountain` objects that you wish to display on the map. Check out the answer in the first link I've provided which gives an example of querying parse for geopoints and then displaying them on a map. – Russell Nov 24 '15 at 16:14
  • Really really thanks Russel. I tried in this days and i figured out how to do this. Thanks again – Luca Dal Seno Nov 27 '15 at 13:58
  • Happy to help and good to have you on SO! Don't forget to mark as answer or upvote if it was helpful :) – Russell Nov 27 '15 at 19:04