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

问题描述 投票:0回答:1

我一直在尝试让Google地图在Android Studio中使用。

[使用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个回答
0
投票

错误通常是由以下任何原因引起的:

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

请确保您正在使用具有有效计费帐户的项目中的API密钥,因为Maps SDK for Android documentation中已说明,您必须先注册并创建一个计费帐户,然后才能开始使用Google Maps Platform API和SDK。 。

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

最后,如果您仍然遇到问题,请随时file a support case打开个性化的沟通渠道。

© www.soinside.com 2019 - 2024. All rights reserved.