简体   繁体   English

如何在Swift 2 [MapKit]中从地图中删除注释

[英]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. 我可以使用“第一个”按钮在MapView上添加注释,但是我想添加一个新选项,该选项允许使用“第二个”按钮删除注释。 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)

}

}

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). 相反,将let CarCoordinates向上移动(并在开头添加更合理的名称和小写字母)。 Then call removeAnnotation on that object. 然后在该对象上调用removeAnnotation。

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

}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM