【问题标题】:PFGeoPoint to save something on map permanently?PFGeoPoint 永久保存地图上的东西?
【发布时间】:2015-11-23 17:04:19
【问题描述】:

我想了解PFGeoPoint 课程。我在 Parse 网站上读到了它,但我得到了一个似乎没有答案的问题。 我需要保存PFGeoPoint 并使其对所有使用我的应用程序的用户可见,我真的不知道为什么。 PFGeoPoint可以吗??

我的应用程序非常简单:您可以登录 (PFUser) 并在地图上标记一个小喷泉(对于正在寻找它的人)。而且我想让所有其他用户(由所有用户)标记的所有喷泉都对所有其他用户可见,除非我从解析中删除它。

@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)

}

我的愿望是在我按下带有当前位置的按钮时设置喷泉,并对其进行评价为好或坏。并将其展示给所有使用 app 的用户。我是 swift 的新手,所以您可能会直接帮助我编写代码。我会很感激的

【问题讨论】:

    标签: ios xcode swift parse-platform


    【解决方案1】:

    PFGeoPoint 绝对是你想要的。为您提供一些背景信息,PFGeoPoint 本质上是 iOS CLLocation 对象的包装,其中包含纬度/经度坐标。

    您需要处理两种情况:

    1. 正在加载附近的所有标记(喷泉)
    2. 创建/保存新标记

    您应该创建另一个 Parse 类,名为 Fountain 或您认为对您的应用最具有描述性的任何内容。此类将包含地理点以及您可能需要的任何其他信息。当用户创建新的Fountain 对象时,分配位置和其他属性(标题、描述等),然后保存。

    在地图本身上,您需要查询Fountain 对象,然后使用这些对象来填充地图。您可能还希望将查询限制在附近的喷泉以提高性能。

    由于这是一个非常开放的问题,这里有一些示例供您参考,欢迎来到 StackOverflow!

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

    MapKit Tutorial with Swift in iOS8

    【讨论】:

    • 谢谢你 Russel,但我对 Swift 还很陌生,正在尝试学习我的第一个应用程序。也许我可以发布代码,以便您更好地帮助我
    • 确保在 Parse 中查询您希望在地图上显示的 Fountain 对象。查看我提供的第一个链接中的答案,该链接提供了一个查询解析地理点然后在地图上显示它们的示例。
    • 真的非常感谢罗素。这几天我试过了,我想出了如何做到这一点。再次感谢
    • 很高兴为您提供帮助,很高兴有您参与!如果有帮助,请不要忘记标记为答案或点赞:)
    猜你喜欢
    • 2018-09-06
    • 2014-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-18
    • 2015-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多