加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

MKPointAnnotations在swift中触摸事件

发布时间:2020-12-14 05:43:27 所属栏目:百科 来源:网络整理
导读:我想知道是否有人能告诉我如何以MKPointAnnotations的形式触摸地图上的图钉. 我想点击地图上的图钉并通过返回我预设的图钉的变量转到另一个视图. 任何人都可以在Swift中解释我这件事吗? 谢谢 使用代码编辑: class ViewController: UIViewController,MKMapV
我想知道是否有人能告诉我如何以MKPointAnnotations的形式触摸地图上的图钉.

我想点击地图上的图钉并通过返回我预设的图钉的变量转到另一个视图.

任何人都可以在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)
    }
}
}
有几个步骤是必要的,这里有一些代码片段可以帮助您入门.

首先,您需要一个自定义类用于您的引脚注释,它包含您要使用的数据.

import MapKit
import Foundation
import UIKit

class PinAnnotation : NSObject,longitude: 0)

    var coordinate: CLLocationCoordinate2D {
        get {
            return coord
        }
    }

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

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

然后你需要一个符合MKMapViewDelegate协议的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!,calloutAccessoryControlTapped control: UIControl!) {
        if let annotation = view.annotation as? PinAnnotation {
            mapView.removeAnnotation(annotation)
        }
    }

这给你这样的东西:

要在地图中添加新注释,请在代码中的某处使用:

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

mapView.addAnnotation(pinAnnotation)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读