我已经创建了函数caricamappa()来加载地图视图和我的mapbox地图,并且在Custompointannotation中有一个控件用于添加和删除注释与Bool控件,一个按钮我想要caricamappa()用力添加和删除注释Bool及其工作,但我不想重新添加子视图(mapview)到我的应用程序,是否可以刷新视图而不添加另一个并隐藏/显示注释?感谢
func carica_mappa() {
// Fill in the next line with your style URL from Mapbox Studio.
let styleURL = NSURL(string: "mapbox:***")
let mapView = MGLMapView(frame: view.bounds,
styleURL: styleURL as URL?)
mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
// Set the map’s center coordinate and zoom level.
mapView.setCenter(CLLocationCoordinate2D(latitude: 44.370417,
longitude: 7.411713),
zoomLevel: 13, animated: false)
view.addSubview(mapView)
mapView.userTrackingMode = .followWithHeading
// Set the delegate property of our map view to `self` after instantiating it.
mapView.delegate = self
let uno = CustomPointAnnotation(coordinate: CLLocationCoordinate2DMake(44.376362, 7.396907),
title: "**",
subtitle: "**",
controllo: visible)
// Set the custom `image` and `reuseIdentifier` properties, later used in the `mapView:imageForAnnotation:` delegate method.
uno.reuseIdentifier = "montagna"
uno.image = UIImage(named: "montagna")
if uno.controllo == true {
mapView.addAnnotation(uno)
}
else {
mapView.removeAnnotation(uno)
}
}
func mapView(_ mapView: MGLMapView, imageFor annotation: MGLAnnotation) -> MGLAnnotationImage? {
if let point = annotation as? CustomPointAnnotation,
let image = point.image,
let reuseIdentifier = point.reuseIdentifier {
if let annotationImage = mapView.dequeueReusableAnnotationImage(withIdentifier: reuseIdentifier) {
// The annotatation image has already been cached, just reuse it.
return annotationImage
} else {
// Create a new annotation image.
return MGLAnnotationImage(image: image, reuseIdentifier: reuseIdentifier)
}
}
// Fallback to the default marker image.
return nil
}
func mapView(_ mapView: MGLMapView, annotationCanShowCallout annotation: MGLAnnotation) -> Bool {
// Always allow callouts to popup when annotations are tapped.
return true
}
override func viewDidLoad() {
super.viewDidLoad()
carica_mappa()
}
答案 0 :(得分:0)
我的建议是拆分代码以设置地图,并添加CustomAnnotationView。 CaricaMappa()只应用于将地图添加到其超级视图并在viewDidLoad()中调用。设置CustomAnnotation的代码应该是自己的方法,你可以通过按钮点击它来调用它。所以你会有类似的东西:
func carica_mappa() {
// Fill in the next line with your style URL from Mapbox Studio.
let styleURL = NSURL(string: "mapbox:***")
let mapView = MGLMapView(frame: view.bounds,
styleURL: styleURL as URL?)
mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
// Set the map’s center coordinate and zoom level.
mapView.setCenter(CLLocationCoordinate2D(latitude: 44.370417,
longitude: 7.411713),
zoomLevel: 13, animated: false)
view.addSubview(mapView)
mapView.userTrackingMode = .followWithHeading
// Set the delegate property of our map view to `self` after instantiating it.
mapView.delegate = self
}
@IBaction func didPressButton(sender: UIButton) {
let uno = CustomPointAnnotation(coordinate: CLLocationCoordinate2DMake(44.376362, 7.396907),
title: "**",
subtitle: "**",
controllo: visible)
// Set the custom `image` and `reuseIdentifier` properties, later used in the `mapView:imageForAnnotation:` delegate method.
uno.reuseIdentifier = "montagna"
uno.image = UIImage(named: "montagna")
if uno.controllo == true {
mapView.addAnnotation(uno)
}
else {
//You might want to check if the annotation exist in the map first.
mapView.removeAnnotation(uno)
}
}