简体   繁体   English

flutter 的谷歌地图和谷歌移动广告不能一起使用

[英]Google maps for flutter and google mobile ads doesn't work together

I created an app which use both google maps flutter plugin and google mobile ads plugin.我创建了一个使用谷歌地图 flutter 插件和谷歌移动广告插件的应用程序。 The problem came only after implementing the mobile ads.问题仅在实施移动广告后出现。 Initially the google map loaded very well and was working fine.最初,谷歌 map 加载得很好并且工作正常。 After installing the mobile ads plugin the problem arose.安装移动广告插件后,问题出现了。

The problem is, on initiation of the app, the map loads (as it loads faster than the adWidget).问题是,在启动应用程序时,map 会加载(因为它的加载速度比 adWidget 快)。 But the moment the adwidget Loads, the whole map becomes black.但是在广告小部件加载的那一刻,整个 map 变成黑色。 Sometimes, the "Google" sign appears on the bottom left.有时,“Google”标志会出现在左下方。 But when I tap on the map and then move the camera, the app seems to load as usual, and the moment I release the map stops loading (or loads only a part of the map).但是当我点击 map 然后移动相机时,应用程序似乎像往常一样加载,而我释放 map 的那一刻停止加载(或仅加载地图的一部分)。 I've tried this without the adWidget, and there is no problem when working without it.我已经在没有 adWidget 的情况下尝试过这个,没有它工作时没有问题。 The problem only arises when the two are together.只有当两者在一起时才会出现问题。 Anyone having the same issue?有人有同样的问题吗?

@highdragon I had the same issue and found a workaround rather than a solution but I hope it will help you. @highdragon 我遇到了同样的问题并找到了解决方法而不是解决方案,但我希望它会对您有所帮助。 The workaround is to display the ad at a menu or any other place where the map isn't showing, it isn't perfect, but for now it will have to do the trick.解决方法是在菜单或 map 未显示的任何其他位置显示广告,它并不完美,但现在它必须解决问题。 Also you can give it a custom size using:你也可以给它一个自定义大小使用:

final AdSize adSize = AdSize(width: 250, height: 100);

And then when inserting the size:然后在插入尺寸时:

 size: adSize,

Where's a full example:哪里有一个完整的例子:

@override
  void didChangeDependencies(){
    final AdSize adSize = AdSize(width: 250, height: 100);
    super.didChangeDependencies();
    final adHelper = Provider.of<AdHelper>(context);
    adHelper.initialization.then((status){
      setState(() {
        banner ??= BannerAd(
        adUnitId: AdHelper.bannerAdUnitId,
        size: adSize,
        request: AdRequest(),
          listener: AdListener(
            onAdLoaded: (Ad ad) {
              print('Ad loaded: $BannerAd.');
              bannerCompleter.complete(ad as BannerAd);
              },

......

And at the place you want to show the ad:在您要展示广告的地方:

......
              if (banner == null)
            SizedBox(height: 50,)
              else
              Container(
                width: 250,
              height: 100,
              child: AdWidget(ad: banner,),
              )

Google Maps now supports hybrid composition as.谷歌地图现在支持混合合成。 of google_maps_flutter: ^2.1.0 , google_maps_flutter: ^2.1.0 ,

from the docs, https://pub.dev/packages/google_maps_flutter to enable Hybrid composition use从文档https://pub.dev/packages/google_maps_flutter启用混合组合使用

if (defaultTargetPlatform == TargetPlatform.android) {
  AndroidGoogleMapsFlutter.useAndroidViewSurface = true;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM