2017-04-02 64 views
0

我已經使用我的mapbox地圖創建了caricamappa()函數來加載地圖視圖,並且在Custompointannotation中有一個用於控制添加和刪除註釋的Bool控件,我想Caricamappa()添加和刪除註釋與武力Bool及其工作,但我不想重新添加子視圖(mapview)到我的應用程序,是否有可能刷新視圖,而無需添加另一個和隱藏/顯示註釋?謝謝不要在viewdidload中加載mapview後重新添加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

我的建議是拆分代碼以設置地圖和添加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) 
     } 
    }