capacitor-plugins icon indicating copy to clipboard operation
capacitor-plugins copied to clipboard

Google Map gets hidden after route change

Open warjohn123 opened this issue 3 years ago • 2 comments

Bug Report

Plugin(s)

Capacitor Version

💊   Capacitor Doctor  💊 

Latest Dependencies:

  @capacitor/cli: 3.5.1
  @capacitor/core: 3.5.1
  @capacitor/android: 3.5.1
  @capacitor/ios: 3.5.1

Installed Dependencies:

  @capacitor/android: 3.4.3
  @capacitor/ios: 3.4.3
  @capacitor/core: 3.4.3
  @capacitor/cli: 3.4.3

[success] iOS looking great! 👌
[success] Android looking great! 👌

Platform(s)

iOS and Android

Current Behavior

Awesome plugin. However I have an issue rendering the map. At first it works fine, but after changing routes and goes back to the route where map is shown, it won't load anymore. Btw, the map is shown in a tab

Expected Behavior

Should not get hidden after route change

Code Reproduction

const mapElement = document.getElementById('my-map');
    const mapConfig = {
      center: {
        lat: 33.6,
        lng: -117.9,
      },
      zoom: 8,
      androidLiteMode: false,
    };
    const mapOptions = {
      id: 'map',
      apiKey: MAP_APIKEY,
      config: mapConfig,
      element: mapElement,
    };
    const map = await GoogleMap.create(mapOptions);

warjohn123 avatar May 22 '22 12:05 warjohn123

Hi warjohn123,

i had the same problem and found my solution in https://github.com/ionic-team/capacitor-plugins/issues/980.

After I have encapsulated the GoogleMap.create in a timeout, the problem was resolved.


ionViewDidEnter() {
    setTimeout(() => {
      this.createMap();
    }, 10);
  }

In my opinion, it's a hard way and only a workaround :-)

There's a problem in the module, i think. So this issue must be proof from the capacitor team.

mwiesmueller avatar Jun 23 '22 18:06 mwiesmueller

setTimeout works but feels very very wrong, especially if you have a lot of places, the tiles from Gmaps have to be loaded each time and you need some functionality to "destroy" the map properly. Our app crashed after several page back and forth transitions, I guess due to memory, even with ngOnDestory to destroy the existing map.

muuvmuuv avatar Sep 20 '22 16:09 muuvmuuv

bump

matteoerrera avatar Apr 09 '23 04:04 matteoerrera

This issue may need more information before it can be addressed. In particular, it will need a reliable Code Reproduction that demonstrates the issue.

Please see the Contributing Guide for how to create a Code Reproduction.

Thanks! Ionitron 💙

Ionitron avatar May 23 '23 13:05 Ionitron

I reproduced this issue with Ionic on iOS.

Other than in Android, where the map is added natively behind the Webview, iOS can access the webview items and append UIViews. iOS workflow:

  • Search all subviews of webview for target container (capacitor-google-maps of type WKChildScrollView)
  • Add the map as UIView to this target container (this also syncs the x and y changes on scroll)

When you now navigate to another page, the page with the map gets hidden with display: none. This causes the map to not show again. I also tested it with safari remote tools and just set display: none to the capacitor-google-maps container and then removed this rule again with the result, that the map is not displayed anymore.

I debugged it with "Capture View Hierarchy" tool in XCode and for whatever reason the target container does not contain the map-controller anymore.

As workaround it would be possible to recreate the map instance whenever an ionic page is shown (i.e. ionViewWillEnter).

AE1NS avatar Jun 01 '23 10:06 AE1NS

I added a fix for this with the help of the ResizeObserver. As I am already implemented it for another feature, I extended the PR https://github.com/ionic-team/capacitor-plugins/pull/1595

AE1NS avatar Jun 01 '23 15:06 AE1NS

I added a fix for this with the help of the ResizeObserver. As I am already implemented it for another feature, I extended the PR #1595

A big thank you for this @AE1NS. I'm not familiar with the process: will this be merged into main within a foreseeable future?

thor9n avatar Aug 09 '23 09:08 thor9n

Cant answer this, I am just a contributor not a maintainer. PR is opened and we have to wait for the ionic team to fully test it so they can decide if it gets applied.

AE1NS avatar Aug 09 '23 09:08 AE1NS

That was probably a coincidence that the PR was merged some hours after your post 😅

AE1NS avatar Aug 10 '23 04:08 AE1NS

closing since https://github.com/ionic-team/capacitor-plugins/pull/1595 got merged and released

jcesarmobile avatar Aug 16 '23 12:08 jcesarmobile