繁体   English   中英

Android 中的 Google Maps API 显示空白屏幕

[英]Google Maps API in Android displays blank screen

我一直在尝试让 Google 地图在 Android Studios 中工作。

使用 Google 地图模板创建新项目时,效果很好。 但是,当我在现有项目中实现地图时,它显示的只是一个灰色屏幕,左下角带有徽标。

现有项目使用一个片段导航系统,其中包含一个承载所有其他片段类的活动。 但这不应该是问题的原因,因为我以与模板相同的方式实现了地图,但它也不起作用。

我检查了 Logcat 输出,密钥验证没有错误。 如果我更改密钥,则会出现错误。

这是包含地图的片段类的代码:

    public class MapFragment extends Fragment implements OnMapReadyCallback {

    private MapViewModel dashboardViewModel;

    private GoogleMap mMap;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        dashboardViewModel =
                ViewModelProviders.of(this).get(MapViewModel.class);
        View root = inflater.inflate(R.layout.fragment_map, container, false);

        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        return root;
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        LatLng testLocation = new LatLng(50, -2);
        mMap.moveCamera(CameraUpdateFactory.newLatLng(testLocation));
    }

}

这是我的清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.backintyne">

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="@string/google_maps_key" />

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

提前致谢

该错误通常可能由以下任何一种原因引起:

  • 使用 API 密钥但来自没有有效结算帐号的项目
  • 使用无效/不正确/格式错误的 API 密钥
  • 使用仅限于您自己以外的其他应用程序的 API 密钥
  • 您的项目未启用 Maps SDK for Android

确保您使用的 API 密钥来自具有有效结算帐户的项目,因为Maps SDK for Android 文档中指出,在开始使用 Google Maps Platform API 和 SDK 之前,您必须注册并创建结算帐户.

如果您的 API 密钥有限制,请确保根据API 密钥最佳实践对其进行适当限制。 确保您已在 GCP 控制台中为您的地图项目启用 Maps SDK for Android。

最后,如果您仍有问题,请随时提交支持案例以打开个性化的沟通渠道。

暂无
暂无

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

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