MKPointAnnotation和PFObject

时间:2016-01-28 00:57:33

标签: swift parse-platform mkpointannotation

摘要:使用来自app的Parse链接(NOT Query)注释

已完成 我尝试做的是拥有新创建的注释,结果成为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

1 个答案:

答案 0 :(得分:0)

我不太清楚你在问什么(例如Linking (NOT Query)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 :OP需要titlesubtitleimage功能。

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
    }

}