简体   繁体   English

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

[英]Google Maps API in Android displays blank screen

I've been trying to get Google maps to work in Android Studios.我一直在尝试让 Google 地图在 Android Studios 中工作。

When creating a new project using the Google maps template, it works fine.使用 Google 地图模板创建新项目时,效果很好。 However when I implement the map in my existing project, all it displays is a grey screen with the logo in the bottom left.但是,当我在现有项目中实现地图时,它显示的只是一个灰色屏幕,左下角带有徽标。

The existing project uses a fragment navigation system with a single activity that hosts all the other fragments classes.现有项目使用一个片段导航系统,其中包含一个承载所有其他片段类的活动。 But this shouldn't be the cause of the issue because I implemented the map the same way as in the template, and it didn't work either.但这不应该是问题的原因,因为我以与模板相同的方式实现了地图,但它也不起作用。

I've checked the Logcat output, and there isn't an error for key validation.我检查了 Logcat 输出,密钥验证没有错误。 If I change the key then the error appears.如果我更改密钥,则会出现错误。

Here's the code for the fragment class that contains the map:这是包含地图的片段类的代码:

    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));
    }

}

This is my manifest file:这是我的清单文件:

<?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>

Thanks in advance提前致谢

The error usually could be caused by any of the following:该错误通常可能由以下任何一种原因引起:

  • using an API key but from a project without a valid billing account使用 API 密钥但来自没有有效结算帐号的项目
  • using an invalid/incorrect/malformed API key使用无效/不正确/格式错误的 API 密钥
  • using an API key which is restricted to other applications aside from your own使用仅限于您自己以外的其他应用程序的 API 密钥
  • Maps SDK for Android is not enabled on your project您的项目未启用 Maps SDK for Android

Make sure you are using an API key from a project that has a valid billing account since it is stated in Maps SDK for Android documentation that before you can start using the Google Maps Platform APIs and SDKs, you must sign up and create a billing account.确保您使用的 API 密钥来自具有有效结算帐户的项目,因为Maps SDK for Android 文档中指出,在开始使用 Google Maps Platform API 和 SDK 之前,您必须注册并创建结算帐户.

If your API key has restrictions, make sure that it is properly restricted according to the API key best practices .如果您的 API 密钥有限制,请确保根据API 密钥最佳实践对其进行适当限制。 Make sure that you have enabled Maps SDK for Android for your maps project in the GCP console.确保您已在 GCP 控制台中为您的地图项目启用 Maps SDK for Android。

Lastly, if you are still having issues, feel free to file a support case to open a personalized communication channel.最后,如果您仍有问题,请随时提交支持案例以打开个性化的沟通渠道。

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

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