我不太清楚你在问什么(例如用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:需要 OPtitle
和subtitle
功能image
。
title
和subtitle
属性不是 独有的,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
}
}