dapriett / nativescript-google-maps-sdk

Cross Platform Google Maps SDK for Nativescript
MIT License
244 stars 164 forks source link

Blank map only after resuming a tabbed app #323

Open public-virtualys opened 5 years ago

public-virtualys commented 5 years ago

I have a problem involving tabs and map.

I have a map in one tab. Everything is all right at first launch, i can see the map and interacting with it with no problems. But if the app is suspended, gone in background for instance, and resumed, i cannot see the map anymore ; i only have a blank grey screen in the tab.

I have no errors in console, and the MapView and GoogleMap objects seem to be still good. This is obviously not an api key problem since the map has been shown at least once. Anybody has an idea of the potential source of the problem ?

ouss4m4 commented 5 years ago

is your mapview on the main route ? my case i have a listview (master) and map rendered in details if i tab the app on the Details screen , this happens, if i route back to master (no mapview) everything is perfect i think i'll route back on application pause, as a work around hope you'll find something for your case

public-virtualys commented 5 years ago

After investigations, it seems that life cycle events are not propagated to the map view. I don't know if tabview or mapview is involved, but manually propagate events seems to correct the problem :

Application.on(Application.suspendEvent, (args) => { if (this._mapView) { this._mapView.nativeview.onPause(); } }); Application.on(Application.resumeEvent, (args) => { if (this._mapView) { this._mapView.nativeview.onResume(); } });

ouss4m4 commented 5 years ago

@public-virtualys in my case i get an error not a blank page,

com.google.maps.api.android.lib6.impl.ao cannot be cast to android.view.viewgroup

sserdyuk commented 5 years ago

Indeed, the map doesn't resume properly when it is used inside TabView because the map component receives and processes "onPause" and then unsubscribes from application event on "unloaded", and when the app is resumed the "onResume" comes first and "loaded" where application events are setup comes second, so the application "onResume" event never fires. I have used the following code inside my component to work around it.

  private mapView: MapView;

  ngOnInit() {
    if (application.android) {
      application.android.on(application.AndroidApplication.activityResumedEvent, this.onAndroidActivityResume, this);
    }
  }

  ngOnDestroy() {
    if (application.android) {
      application.android.off(application.AndroidApplication.activityResumedEvent, this.onAndroidActivityResume, this);
    }
  }

  private onAndroidActivityResume(args) {
    if (this.mapView && this.mapView.nativeView && this.mapView._context === args.activity) {
      this.mapView.nativeView.onResume();
    }
  }

  onMapReady(event) {
    this.mapView = event.object;
    // other setup
  }
MCorzo commented 5 years ago

@sserdyuk thank you man, your code snippet work really well

joshcomley commented 4 years ago

@sserdyuk Thank you, this has helped me, too