MKPointAnnotations touch event in swift

寵の児 提交于 2019-11-30 15:21:05

问题


I would like to know if anyone can tell me how I can touch a pin on the map in the form of MKPointAnnotations .

I would like to click the pin on the map and go to another view by bringing back the variables of the pin that I have preset .

Can anyone explain me this thing in Swift ?

thanks

Edit with code:

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)
    }
}
}

回答1:


Several steps are necessary, here are some code snippets to get you started.

First you need a custom class for your pin annotation which holds the data you want to work with.

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!
        }
    } 
}

Then you need a custom class for your MKMapView which conforms to the MKMapViewDelegate protocol. Implement the method viewForAnnotation there:

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)
        }
    }

That gives you something like this:

To add a new annotation to your map use this somewhere in your code:

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

mapView.addAnnotation(pinAnnotation)



回答2:


Greate work !!! BUT.. I just copy past this and I had to add a few changes. I'll share with you guys those changes.

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 title: String? {
    get {
        return _title
    }
    set (value) {
        self._title = value!
    }
}

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

var coordinate: CLLocationCoordinate2D {
    get {
        return coord
    }
}

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

I hope it will help :D



来源:https://stackoverflow.com/questions/26991473/mkpointannotations-touch-event-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!