MKPointAnnotations touch event in swift
Asked Answered
W

2

9

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)
    }
}
}
Worsted answered 18/11, 2014 at 10:2 Comment(4)
If the annotation's title is blank, callout will not display when you tap on it.Mier
I added the annotation , and a written text , the popup comes out properly but does nothing .. you know why?Worsted
Maybe the map view's delegate outlet is not connected/set.Mier
I confirm that it is connected, more than anything else , I wonder where I match the method MapView , are new to the language and can not understand how he can leave this method if you do not call ... thanksWorsted
M
24

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:

enter image description here

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

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

mapView.addAnnotation(pinAnnotation)
Murtagh answered 18/11, 2014 at 10:36 Comment(8)
Hello , in viewDidLoad to load the point on the map as the septum ? I can not figure out how to tell the map to add the pinWorsted
I have edited my answer to show how to add a new pin to the map.Murtagh
I edited my question with my code , now I see the pin on the map , but if I click does nothing , no tooltip appears , how do I fix ?Worsted
It is called automatically because it is a delegate function. Make sure the your custom class is set as the mapview's delegate.Murtagh
I set the delegate of MKMapView pointing to my ViewController , I have also extended the class , as you can see in the above code . What else have I forgotten? the method does not automatically MapViewWorsted
Do you have a trash image as in my example? If not, remove the code which creates the leftCalloutAccessory and see if it works.Murtagh
yes yes I put the picture , my problem is that the method does not automatically MapView , I dragged the delegates of mkmapview ... added to the controller extension class and even costresso it with self.maps.delegate = self .... at this point I do not know what to do :)Worsted
Thanks for the effort you 've marked the question right , I made ​​some changes and now goesWorsted
G
5

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

Guarnerius answered 9/6, 2016 at 10:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.