首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Swift 2 [MapKit]中从地图中删除注释

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

Stack Overflow用户
提问于 2015-11-17 21:59:42
回答 2查看 2.8K关注 0票数 1

我可以使用"first“按钮在MapView上添加注释,但我想添加一个新选项,它允许使用”第二“按钮删除注释。添加注释没有问题,但我不能从map视图中删除它。有人能帮我吗?

这是我的代码:

代码语言:javascript
复制
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)

}

}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-11-17 23:13:38

我认为问题在于:

代码语言:javascript
复制
self.mapViewOutlet.removeAnnotation(CarAdnotaion(coordinate: (lm.location?.coordinate)!))

您正在删除刚才在这个状态器中创建的注释。相反,将let CarCoordinates向上移动(并在开头给它更合理的名称和小写字母)。然后调用该对象上的removeAnnotation。

票数 1
EN

Stack Overflow用户

发布于 2015-11-18 18:46:46

我有办法解决我的问题。它起作用了!

代码语言:javascript
复制
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!)

}

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33767833

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档