【问题标题】:Google Maps API in Android displays blank screenAndroid 中的 Google Maps API 显示空白屏幕
【发布时间】:2020-06-12 18:57:49
【问题描述】:

我一直在尝试让 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>

提前致谢

【问题讨论】:

    标签: java android android-studio google-maps-android-api-2


    【解决方案1】:

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

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

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

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

    最后,如果您仍有问题,请随时file a support case 开启个性化沟通渠道。

    【讨论】:

      猜你喜欢
      • 2012-11-25
      • 1970-01-01
      • 2021-12-04
      • 1970-01-01
      • 1970-01-01
      • 2013-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多