swift - swift 中的 MKPointAnnotations 触摸事件

标签 swift view mapkit mkpointannotation

我想知道是否有人可以告诉我如何以 MKPointAnnotations 的形式触摸 map 上的图钉。

我想点击 map 上的 pin 并通过返回 pin 的 variables 转到另一个 View 我已经预设了。

谁能用 Swift 给我解释一下?

谢谢

用代码编辑:

class ViewController: UIViewController, MKMapViewDelegate {


@IBOutlet weak var mappa: MKMapView!


override func viewDidLoad() {
    super.viewDidLoad()

    var location : CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 44.648590, longitude: 10.918794)

    let pinAnnotation = PinAnnotation()
    pinAnnotation.setCoordinate(location)

    self.mappa.addAnnotation(pinAnnotation)

}

class PinAnnotation : NSObject, MKAnnotation {
    private var coord: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 0, longitude: 0)

    var coordinate: CLLocationCoordinate2D {
        get {
            return coord
        }
    }

    var title: String = "test"
    var subtitle: String = "test"

    func setCoordinate(newCoordinate: CLLocationCoordinate2D) {
        self.coord = newCoordinate
    }        
}

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
    if annotation is PinAnnotation {
        let pinAnnotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "myPin")

        pinAnnotationView.pinColor = .Purple
        pinAnnotationView.draggable = true
        pinAnnotationView.canShowCallout = true
        pinAnnotationView.animatesDrop = true

        let deleteButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
        deleteButton.frame.size.width = 44
        deleteButton.frame.size.height = 44
        deleteButton.backgroundColor = UIColor.redColor()
        deleteButton.setImage(UIImage(named: "trash"), forState: .Normal)

        pinAnnotationView.leftCalloutAccessoryView = deleteButton


        return pinAnnotationView
    }

    return nil
}

func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) {
    if let annotation = view.annotation as? PinAnnotation {
        self.mapView.removeAnnotation(annotation)
    }
}
}

最佳答案

需要几个步骤,这里有一些代码片段可以帮助您入门。

首先,您需要一个用于 pin 注释的自定义类,其中包含您要使用的数据。

import MapKit
import Foundation
import UIKit

class PinAnnotation : NSObject, MKAnnotation {
    private var coord: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 0, longitude: 0)
    private var _title: String = String("")
    private var _subtitle: String = String("")

    var coordinate: CLLocationCoordinate2D {
        get {
            return coord
        }
    }

    func setCoordinate(newCoordinate: CLLocationCoordinate2D) {
        self.coord = newCoordinate
    }   

     var title: String? {
        get {
            return _title
        }
        set (value) {
            self._title = value!
        }
    }

    var subtitle: String? {
        get {
            return _subtitle
        }
        set (value) {
            self._subtitle = value!
        }
    } 
}

然后您需要一个符合MKMapViewDelegate 协议(protocol)的MKMapView 自定义类。在那里实现方法 viewForAnnotation:

import MapKit
import CLLocation
import Foundation
import UIKit

class MapViewController: UIViewController, MKMapViewDelegate {

    ...

    func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
        if annotation is PinAnnotation {
            let pinAnnotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "myPin")

            pinAnnotationView.pinColor = .Purple
            pinAnnotationView.draggable = true
            pinAnnotationView.canShowCallout = true
            pinAnnotationView.animatesDrop = true

            let deleteButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
            deleteButton.frame.size.width = 44
            deleteButton.frame.size.height = 44
            deleteButton.backgroundColor = UIColor.redColor()
            deleteButton.setImage(UIImage(named: "trash"), forState: .Normal)

            pinAnnotationView.leftCalloutAccessoryView = deleteButton

            return pinAnnotationView
        }

        return nil
    }

    func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) {
        if let annotation = view.annotation as? PinAnnotation {
            mapView.removeAnnotation(annotation)
        }
    }

这给了你这样的东西:

enter image description here

要向您的 map 添加新注释,请在代码中的某处使用:

let pinAnnotation = PinAnnotation()
pinAnnotation.setCoordinate(location)

mapView.addAnnotation(pinAnnotation)

关于swift - swift 中的 MKPointAnnotations 触摸事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26991473/

相关文章:

ios - UITextField 圆角文本

mysql - 所有 MySQL 设计者在更改 View 时都会生成错误代码

view - 模态视图的导航栏位置 - iOS7

android - 使用 match_parent 属性获取 View 宽度?

iphone - 地理围栏指标与位置服务指标

ios - 同一应用中的 Google Maps SDK 和 Mapkit 导致崩溃

ios - 快速从单独的 nib 文件加载 UIViewController?

ios - UITextView 根据 UITableViewCell 中的文本动态宽度

ios - 无法找到 respondsToSelector :@selector(charValue) in Swift 3 的等价物

ios - 监控 MKMapView 重绘事件