首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MKPointAnnotation和PFObject

MKPointAnnotation和PFObject
EN

Stack Overflow用户
提问于 2016-01-28 00:57:32
回答 1查看 207关注 0票数 0

摘要:从app链接(而不是查询)注释

Completed:我想做的是让新创建的注释成为一个PFObject。我尝试这样做的方式是获得基于MKPointAnnotation的坐标。然后将其转换为具有坐标的PFObject。然后能够稍后作为一个实时更新进行查询。

Problem仍然是一个问题,由于上面提到的代码,我无法拥有允许我添加标题和UIImage的MKPointAnnotation,除了我需要提示的是将所有这些合并在一起,最终能够从解析中被查询。

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-28 17:08:30

我不太清楚您在问什么(例如,使用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是什么意思)。

我尝试用以下三个文件组合一个示例项目。由于您可以使用以下三行代码来实现上面概述的两个目标,所以它非常干净:

代码语言:javascript
复制
let myAnnotation = MyAnnotation(coordinate: coordinate)
mapView.addAnnotation(myAnnotation)
myAnnotation.saveInBackground() // Handle this background save better

如果这对你的问题有帮助,请告诉我,否则,请把你的问题重新表述清楚。

更新1:OP需要titlesubtitleimage功能。

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

关于图像,这不是MKPinAnnotationView的唯一特性,而是更通用的MKAnnotationView。实际上,image属性在MKPinAnnotationView中没有用途。如image属性的文档所述:

代码语言:javascript
复制
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属性,您需要在委托方法mapView:viewForAnnotation: from MKMapViewDelegate中使用MKAnnotationView

记住设置mapView的委托属性(我已经在故事板中设置了它)。

我更新了我的答案,以反映变化。

守则:

代码语言:javascript
复制
//
//  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
    }

}    
代码语言:javascript
复制
//
//  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"

}
代码语言:javascript
复制
//
//  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
    }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35051069

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档