2017-04-09 3 views
0

Ich habe diese zwei Funktionen erstellt und wenn ich Funktion pippo() aufrufen, erhalte ich Fehler "fataler Fehler: unerwartet gefunden Null beim Entpacken ein optionaler Wert", zur Behebung dieses Fehlers habe ich diese mapView geändert .addAnnotation (hallo) in mapView? .addAnnotation (Hallo), aber ich zeige die Annotation nicht in die Karte, wenn ich pippo innerhalb von caricamappa anrufe, wird die Annotation erfolgreich hinzugefügt, aber ich möchte Funktion laden Karten und Annotation hinzufügen, ist es möglich?Hinzufügen Annotation in mapView mit zwei Funktionen

> func caricamappa(){ 
>  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: 46.370417, 
>            longitude: 8.411713), 
>      zoomLevel: 13, animated: false) 
>  view.addSubview(mapView) 
>  mapView.delegate = self 

>  } 
> 
> func pippo(){ 
>  let hello = MGLPointAnnotation() 
>  hello.coordinate = CLLocationCoordinate2D(latitude: 46.376362, longitude: 8.396907) 
>  hello.title = "Hello world!" 
>  hello.subtitle = "Welcome to my marker" 
>  // Add marker `hello` to the map. 
>  mapView.addAnnotation(hello) 
>  
>  } 
> 
> 
> 
> override func viewDidLoad() { 
>  super.viewDidLoad() 
>  caricamappa() 
>  pippo() 
>  

Antwort

0

Sie definieren mapView in caricamappa, damit es nicht statt in pippo existieren, machen mapView eine IBOutlet außerhalb Ihrer Methoden.

+0

Ok danke !! Jetzt sehe ich die Annotation, aber ich muss Funktion func mapView verwenden (_ mapView: MGLMapView, imageFor Annotation: MGLAnnotation) -> MGLAnnotationImage? { wenn Punkt = Annotation als? CustomPointAnnotation, und in Pippo ist nicht möglich, diese Funktion zu verwenden, richtig? –

+0

Es tut mir leid, ich verstehe Ihre Frage nicht. Könnten Sie Ihrem ursprünglichen Beitrag mehr Code hinzufügen und ich werde versuchen, Ihnen zu helfen. – Magnas

Verwandte Themen