ionic-team / ionic-native-google-maps

Google maps plugin for Ionic Native
Other
221 stars 125 forks source link

Plugin crashing when Ionic app is initialized in the background #45

Closed giladrom closed 6 years ago

giladrom commented 6 years ago

I'm submitting a ... (check one with "x")

If you choose 'problem or bug report', please select OS: (check one with "x")

cordova information: (run $> cordova plugin list)

Current behavior:

This is just a solution description to help anyone else who might be encountering this problem (Not sure it's a bug) - when developing an Ionic/Cordova app that has any kind of background activity which might cause the app to "wake up" in the background (iBeacon, for example) after it has been terminated by the user or iOS, and attempt to initialize the map inside ionViewDidLoad(), the plugin will crash with the following error after about 30 seconds (retrieved from Ionic Pro console, only generic error in the logs):

TypeError: undefined is not an object (evaluating 'map.getDiv')
at _remove (CordovaGoogleMaps.js:714:16)
at ? ([native%20code]::)
at callback (BaseClass.js:113:21)
at trigger (BaseClass.js:68:25)
at remove (Map.js:655:15)
at ? (vendor.js:1:679554)
at onInvokeTask (vendor.js:1:76614)
at runTask (polyfills.js:3:10844)
at invokeTask (polyfills.js:3:16801)
at n (polyfills.js:3:66)

This happens because there is no DIV element to attach to, even though the app itself is running, and the map appears to have been initialized successfully and without an error.

The solution is to only initialize the app when ionViewDidEnter() is called, and not on ionViewDidLoad() like in the example on the main page. For example, use an Observable to initialize the map view when the page is first opened:

private created$: Subject<boolean> = new Subject();

ionViewDidEnter() {
    this.created$.next(true);
    this.created$.complete();
}

ionViewDidLoad() {
    this.created$.subscribe(() => {
      this.initializeMapView();
    });
}

Related code, data or error log (please format your code or data):

wf9a5m75 commented 6 years ago

there is no DIV element to attach to ...

You have to specify div that is under the body.

giladrom commented 6 years ago

The DIV is there. If you read my report you will see that this only happens when the app is started in the background.

wf9a5m75 commented 6 years ago

the app is started in the background.

Not supported.

giladrom commented 6 years ago

Not supported.

This is not a support request, just something you could add to the documentation to make sure people don't waste time on this like I did. Maybe someone else can find this useful.

wf9a5m75 commented 6 years ago

Yes, I understand. There is no same report so far, and you don't provide enough crash log (logcat). So I just say, thank you for reporting.