gpt4 book ai didi

ios - 在viewdidload中加载后不要重新添加mapview

转载 作者:行者123 更新时间:2023-11-30 12:33:22 25 4
gpt4 key购买 nike

我已经创建了函数 caricamappa() 来使用我的 mapbox map 加载 map View ,并且在 Custompointannotation 中有一个用于使用 Bool 控件添加和删除注释的控件,其中有一个按钮我想要 caricamappa() 强制添加和删除注释Bool 及其工作,但我不想将 subview (mapview)重新添加到我的应用程序中,是否可以刷新 View 而不添加另一个 View 并隐藏/显示注释?谢谢

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()
}

最佳答案

我的建议是分开设置 map 的代码和添加 CustomAnnotationView 的代码。 CaricaMappa() 只能用于将 map 添加到其 super View 并在 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)
}
}

关于ios - 在viewdidload中加载后不要重新添加mapview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43166290/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com