Ionic 谷歌Map不显示在android

t30tvxxf  于 2022-12-08  发布在  Ionic
关注(0)|答案(1)|浏览(193)

我有一个应用程序,我在ionic @capacitor/google中使用maps javascript API来显示Map。当我在浏览器中运行该应用程序时,它工作正常。但当我在模拟器或设备中运行该应用程序时,Map不显示。
我正在使用的方法来显示谷歌Map:

async createMap(map: ElementRef) {
this.map = await GoogleMap.create({
  id: "capacitor-google-maps",
  element: map.nativeElement,
  apiKey: environment.googleMapsApiKey,
  config: {
    zoom: 18,
    center: this.position,
    mapTypeId: "satellite",
    disableDefaultUI: true,
  },
  forceCreate: true,
});

await this.addMarker(this.position.lat, this.position.lng);
await this.addListeners();
}
l7wslrjt

l7wslrjt1#

On the documentation it says:
On Android, the map is rendered beneath the entire webview, and uses this component to manage its positioning during scrolling events. This means that as the developer, you must ensure that the webview is transparent all the way through the layers to the very bottom. In a typically Ionic application, that means setting transparency on elements such as IonContent and the root HTML tag to ensure that it can be seen. If you can't see your map on Android, this should be the first thing you check.
So you need to alter your css so that it shows on Android, something like this should get the job done:

ion-content {
  --background: transparent;
}

body {
  background: transparent;
}

However, doing these css changes will cause a lot of unwanted style changes and issues. Moreover, based on my experience trying @capacitor/google-maps, I would highly recommend using @angular/google-maps instead, as it is more developed, maintained by google and the capacitor plugin still has a long way to go.
You can check this post on ionic's forum as I had the exact same issue.

UPDATE:

You can check @angular/google-maps here , also you can check below a sample code:
HTML

<google-map [center]="{lat: 20, lng:20}" [options]="options" height="140px" width="auto">

    <map-marker [position]="{lat: 20, lng: 20}" [options]="markerOptions"></map-marker>

</google-map>

ts:

markerOptions: google.maps.MarkerOptions = {
    draggable: false,
  };

  options: google.maps.MapOptions = {
    zoom: 13,
    disableDefaultUI: true,
    draggable: false,
    clickableIcons: false, 
  };

You can also see Google's documentation to get a list of all the options for their map

相关问题