MKPointAnnotation 和 PFObject
MKPointAnnotation and PFObject
摘要:使用来自应用程序的解析链接(不是查询)注释
已完成:
我想做的是让新创建的注释成为一个 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
我不太清楚你在问什么(例如 Linking (NOT Query)
或 become a PFObject as a result
)。
我了解到您的问题如下:
- 用户创建了一系列
CLLocationCoordinate2D
- 您想轻松地将这些坐标添加到
MKMapView
- 您想轻松地将这些坐标存储在 Parse 后端
我认为对你来说最好的解决方案是创建一个自定义 class 子class es 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 需要 title
、subtitle
和 image
功能。
title
和 subtitle
属性不是 MKPointAnnotation
独有的,而是 MKAnnotation
协议的一部分,用于创建 [=29] =] 在我的例子中。这意味着您只需 将这两个属性添加到 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
属性,您需要在 MKMapViewDelegate
的委托方法 mapView:viewForAnnotation:
中使用 MKAnnotationView
].
记得设置mapView
的delegate属性(我已经在storyboard里设置了)。
我已经更新了我的答案以反映这些变化。
代码:
//
// 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
}
}
摘要:使用来自应用程序的解析链接(不是查询)注释
已完成: 我想做的是让新创建的注释成为一个 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
我不太清楚你在问什么(例如 Linking (NOT Query)
或 become a PFObject as a result
)。
我了解到您的问题如下:
- 用户创建了一系列
CLLocationCoordinate2D
- 您想轻松地将这些坐标添加到
MKMapView
- 您想轻松地将这些坐标存储在 Parse 后端
我认为对你来说最好的解决方案是创建一个自定义 class 子class es 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 需要 title
、subtitle
和 image
功能。
title
和 subtitle
属性不是 MKPointAnnotation
独有的,而是 MKAnnotation
协议的一部分,用于创建 [=29] =] 在我的例子中。这意味着您只需 将这两个属性添加到 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
属性,您需要在 MKMapViewDelegate
的委托方法 mapView:viewForAnnotation:
中使用 MKAnnotationView
].
记得设置mapView
的delegate属性(我已经在storyboard里设置了)。
我已经更新了我的答案以反映这些变化。
代码:
//
// 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
}
}