0

摘要:使用 Parse 从应用程序链接(非查询)注释

已完成: 我要做的是让新创建的注释成为一个 PFObject 结果。我尝试这样做的方法是根据 MKPointAnnotation 获取坐标。然后把它变成一个带有坐标的PFObject。然后可以稍后作为实时更新进行查询。

问题 还有一个问题是,使用提到的代码我无法拥有允许我添加标题和 UIImage 的 MKPointAnnotation,除此之外,我需要的所有提示是将所有这些组合在一起最终能够从解析中查询。

func handleLongPress(getstureRecognizer : UIGestureRecognizer) {
    if getstureRecognizer.state != .Began { return }

    let touchPoint = getstureRecognizer.locationInView(self.mapView)
    let touchMapCoordinate = mapView.convertPoint(touchPoint, toCoordinateFromView: mapView)
    createAnnotation(touchMapCoordinate)

}

private func createAnnotation(coordinate: CLLocationCoordinate2D) {
    print("createAnnotation")
    let myAnnotation = MyAnnotation(coordinate: coordinate)
    mapView.addAnnotation(myAnnotation)
    myAnnotation.saveInBackground()


}

    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
        if !(annotation is MKPointAnnotation) {
    return nil
}

let reuseId = "test"

var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
if pinView == nil {
    pinView = MyAnnotation(annotation: annotation, reuseIdentifier: reuseId)
    pinView!.image = UIImage(named:"Cloud9")
    pinView!.canShowCallout = true

    let rightButton: AnyObject! = UIButton(type: UIButtonType.ContactAdd)
    pinView?.rightCalloutAccessoryView = rightButton as? UIView
4

1 回答 1

0

我不太清楚你在问什么(例如用Linking (NOT Query)or become a PFObject as a result)。

我已将您的问题理解为以下内容:

  • 一系列CLLocationCoordinate2D由用户创建
  • 您想轻松地将这些坐标添加到MKMapView
  • 您想轻松地将这些坐标存储在 Parse 后端

我认为对你来说最好的解决方案是创建一个自定义类,它是子类PFObject和实现MKAnnotation(我不太清楚你的意思是什么and that the only solution is to subclass the PFObject, which at its current state there is no help with that either。)

我试图将一个示例项目与以下三个文件放在一起。它既漂亮又干净,因为您可以使用以下三行代码来实现上述两个目标:

let myAnnotation = MyAnnotation(coordinate: coordinate)
mapView.addAnnotation(myAnnotation)
myAnnotation.saveInBackground() // Handle this background save better

让我知道这是否对您的问题有帮助,否则请重新表述您的问题以更清楚。


更新 1:需要 OPtitlesubtitle功能image

titlesubtitle属性不是 独有的,MKPointAnnotation而是协议的一部分,在我的示例MKAnnotation中用于创建。MyAnnotation这意味着您只需添加这两个属性即可MyAnnotation获得所需的功能。

关于图像,这不是 . 的独特功能MKPinAnnotationView,而是更通用的MKAnnotationView. 在现实中,image财产并没有目的MKPinAnnotationView。如该image属性的文档中所述:

You can use the `MKAnnotationView` class as is or subclass it to provide custom behavior as needed. 
The `image` property of the class lets you set the appearance of the annotation view without subclassing directly.

使用 时MKPinAnnotationView,视图将始终是一个引脚,这就是该image属性没有用途的原因。

所以为了使用这个image属性,你需要MKAnnotationView在委托方法mapView:viewForAnnotation:中使用 MKMapViewDelegate.

请记住设置的委托属性mapView(我已在情节提要中设置)。

我已经更新了我的答案以反映这些变化。


编码:

//
//  ViewController.swift
//  ParseAnnotationFun
//
//  Created by Stefan Veis Pennerup on 28/01/16.
//  Copyright © 2016 Kumuluzz. All rights reserved.
//

import UIKit
import MapKit

class ViewController: UIViewController, MKMapViewDelegate {

    // MARK: - Storyboard outlets

    @IBOutlet weak var mapView: MKMapView!

    // MARK: - Gesture Recognizers

    @IBAction func longPressed(sender: UILongPressGestureRecognizer) {
        print("longPressed")
        if sender.state != .Began { return }

        let touchPoint = sender.locationInView(mapView)
        let touchMapCoordinate = mapView.convertPoint(touchPoint, toCoordinateFromView: mapView)
        createAnnotation(touchMapCoordinate)
    }

    // MARK: - Model

    private func createAnnotation(coordinate: CLLocationCoordinate2D) {
        print("createAnnotation")
        let myAnnotation = MyAnnotation(coordinate: coordinate)
        mapView.addAnnotation(myAnnotation)
        myAnnotation.saveInBackground() // Handle this background save better
    }

    // MARK: - MKMapViewDelegate

    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
        let id = "someIdentifier"
        var view = mapView.dequeueReusableAnnotationViewWithIdentifier(id)

        if view == nil {
            view = MKAnnotationView(annotation: annotation, reuseIdentifier: id)
            view?.canShowCallout = true
            view?.image = UIImage(named: "oranges")
        }

        view?.annotation = annotation
        return view
    }

}    

//
//  MyAnnotation.swift
//  ParseAnnotationFun
//
//  Created by Stefan Veis Pennerup on 28/01/16.
//  Copyright © 2016 Kumuluzz. All rights reserved.
//

import Foundation
import Parse
import MapKit

class MyAnnotation: PFObject, PFSubclassing, MKAnnotation {

    // MARK: - Properties

    @NSManaged var location: PFGeoPoint

    // MARK: - Initializers

    init(coordinate: CLLocationCoordinate2D) {
        super.init()
        location = PFGeoPoint(latitude: coordinate.latitude, longitude: coordinate.longitude)
    }

    override class func initialize() {
        struct Static {
            static var onceToken : dispatch_once_t = 0;
        }
        dispatch_once(&Static.onceToken) {
            self.registerSubclass()
        }
    }

    // MARK: - PFSubclassing protocol

    static func parseClassName() -> String {
        return "AnnotationPins"
    }

    // MARK: - MKAnnotation protocol

    var coordinate: CLLocationCoordinate2D {
        return CLLocationCoordinate2DMake(location.latitude, location.longitude)
    }

    var title: String? = "Awesome title"

    var subtitle: String? = "Random subtitle"

}

//
//  AppDelegate.swift
//  ParseAnnotationFun
//
//  Created by Stefan Veis Pennerup on 28/01/16.
//  Copyright © 2016 Kumuluzz. All rights reserved.
//

import UIKit
import Parse

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        MyAnnotation.initialize()
        Parse.setApplicationId("xxx",
        clientKey: "xxx")

        return true
    }

}
于 2016-01-28T17:08:30.017 回答