如何从Swift 2中的Map中删除Annotation [MapKit]

时间:2015-11-17 21:59:42

标签: ios iphone swift mapkit swift2

我可以使用"第一个"在MapView上添加注释。按钮,但我想添加一个新选项,可以使用" second"删除注释。按钮。我没有添加注释的问题,但我无法从地图视图中删除它。有人可以帮助我吗?

这是我的代码:

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)

}

}

2 个答案:

答案 0 :(得分:1)

我认为问题在于:

let CarCoordinates

您正在删除您刚刚在这位政治家中创建的注释。相反,向上移动double d = Double.parseDouble("\u200E-73.04"); Exception in thread "main" java.lang.NumberFormatException: For input string: "?-73.04" (并在开头给它更明智的名字和小写字母)。然后在该对象上调用removeAnnotation。

答案 1 :(得分:0)

我解决了我的问题。它有效!

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

}

}