2017-04-02 9 views
0

マップビューをマップマップにロードするためのcaricamappa()関数を作成し、CustompointannotationにBoolコントロールを使用したボタンの追加と削除のコントロールがあります私はcaricamappa()を追加し、強制的なBoolとその仕事で注釈を削除するが、私は私のアプリにサブビュー(mapview)を再追加したくない、別のものを追加せずにビューをリフレッシュすることは可能ですか?おかげviewdidloadにロードされた後に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) 
     } 
    } 
関連する問題