我可以使用“第一个”按钮在Map视图上添加注解,但我想添加一个新选项,该选项允许使用“第二个”按钮删除注解。我添加注解没有问题,但我无法从Map视图中删除注解。有人能帮助我吗?
下面是我的代码:
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条答案
按热度按时间0kjbasz61#
我认为问题就在这里:
您正在删除刚刚在此语句中创建的注解。相反,请将
let CarCoordinates
上移(并在开头给予它更合理的名称和小写字母)。然后在该对象上调用removeAnnotation。hec6srdp2#
我有办法解决我的问题。它很有效!