Berike
Berike

Reputation: 39

How to remove Annotation from Map in Swift 2 [MapKit]

I can add Annotation on MapView using "first" button, but I would like to add a new option which enables the removing of the annotation using the "second" button. I have no problem with adding annotation, but I cannot remove it from the map view. Can somebody help me in it?

Here is my code:

class ViewController: UIViewController {

@IBOutlet weak var mapViewOutlet: MKMapView!

var lm = CLLocationManager()

override func viewDidLoad() {
    super.viewDidLoad()


    self.lm.requestAlwaysAuthorization()
    mapViewOutlet.showsUserLocation = true
    mapViewOutlet.userTrackingMode = MKUserTrackingMode.Follow

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// HERE IS FUNCTION ADDANNOTATION

@IBAction func tagMyCar(sender: UIButton) {

    let coordinate = lm.location?.coordinate


    let CarCoordinates = CarAdnotaion(coordinate: coordinate!)

    mapViewOutlet.addAnnotation(CarCoordinates)

    let region = CLCircularRegion(center: coordinate!, radius: 5, identifier: "My Car")

    region.notifyOnEntry = true
    region.notifyOnExit = true

    lm.startMonitoringForRegion(region)

}

// HERE IS FUNCTION REMOVEANNOTATION

@IBAction func removeAnnotationfromMap(sender: AnyObject) {

    let region = CLCircularRegion(center: (lm.location?.coordinate)!, radius: 5, identifier: "Mój samochód")

    self.mapViewOutlet.removeAnnotation(CarAdnotaion(coordinate: (lm.location?.coordinate)!))

    lm.stopMonitoringForRegion(region)

}

}

Upvotes: 1

Views: 2796

Answers (2)

Berike
Berike

Reputation: 39

I have a solution of my problem. It works!

import UIKit
import CoreLocation
import MapKit

class ViewController: UIViewController {

@IBOutlet weak var mapViewOutlet: MKMapView!

let lm = CLLocationManager()
var carAnnotation: CarAnnotationOnMap?
var region: CLCircularRegion?


override func viewDidLoad() {
    super.viewDidLoad()

    self.lm.requestAlwaysAuthorization()


    mapViewOutlet.userTrackingMode = MKUserTrackingMode.Follow
    mapViewOutlet.showsUserLocation = true

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// ADD ANNOTATION ON MAP

@IBAction func tagMyCar(sender: UIButton) {

    let coordinates = lm.location?.coordinate
    carAnnotation = CarAnnotationOnMap(coordinates: coordinates!)

    region = CLCircularRegion(center: coordinates!, radius: 5, identifier: "My Zone")


    region!.notifyOnEntry = true
    region!.notifyOnExit = true
    self.lm.startMonitoringForRegion(region!)

    mapViewOutlet.addAnnotation(carAnnotation!)

}

// REMOVE ANNOTATION FROM MAP

@IBAction func removePinFromMyCar(sender: AnyObject) {
    self.lm.stopMonitoringForRegion(region!)
    mapViewOutlet.removeAnnotation(carAnnotation!)

}

}

Upvotes: 0

MirekE
MirekE

Reputation: 11555

I think the problem is here:

self.mapViewOutlet.removeAnnotation(CarAdnotaion(coordinate: (lm.location?.coordinate)!))

You are removing annotation that you just created in this very statemen. Instead, move let CarCoordinates up (and give it more sensible name and lowercase letter in the beginning). Then call removeAnnotation on that object.

Upvotes: 1

Related Questions