【问题标题】:My Google Maps fragment displays gray. I can't get the actual map to appear我的 Google 地图片段显示为灰色。我无法显示实际地图
【发布时间】:2020-08-25 14:48:15
【问题描述】:

我正在尝试将 Google 地图片段添加到选项卡式布局,但地图仅显示为灰色片段,左下角带有 Google 徽标。我的 API 密钥有效,并且我已通过 Google Cloud Platform 启用 API 和 SDK。

MapFragment.java

public class MapFragment extends Fragment implements OnMapReadyCallback {

    private GoogleMap mMap;
    private View mView;
    MapView mapView;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        mapView = getView().findViewById(R.id.mapView);

        mapView.onCreate(savedInstanceState);
        mapView.onResume();

        mapView.getMapAsync(this);
    }

    public MapFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        mView = inflater.inflate(R.layout.fragment_map, container, false);
        return mView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mapView = (MapView) mView.findViewById(R.id.map);
        if(mapView != null) {
            mapView.onCreate(null);
            mapView.onResume();
            mapView.getMapAsync(this);
        }
    }

    @Override
    public void onMapReady(GoogleMap map) {

        mMap = map;
        mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);

    }

fragment_map.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/frameLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MapFragment">

    <com.google.android.gms.maps.MapView
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:apiKey="AIzaSyC-MdT7T9uXkL0GmpbsMeWJsBwhorqAc3c"
        android:clickable="true"
        android:focusable="true"
        android:visibility="visible"
        tools:layout_conversion_absoluteHeight="603dp"
        tools:layout_conversion_absoluteWidth="411dp"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="0dp" />
</FrameLayout>

Screenshot of my app.

解决方案 很多时候错误与凭据有关。尽管在 Google Cloud 中启用了所有必要的 API,但我重新制作了凭据并且错误仍然存​​在。错误是尽管更改了 API 密钥,但原始凭据仍保留在模拟器上的应用程序中。在模拟器(或设备)上,转到Settings&gt;Application Manager&gt;Select Your Application &gt; Click on Clear Data

另一种解决方案是完全卸载应用程序并在重新制作凭据后重新加载。

【问题讨论】:

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


    【解决方案1】:

    解决方案

    很多时候错误与凭据有关。尽管在 Google Cloud 中启用了所有必要的 API,但我重新制作了凭据并且错误仍然存​​在。错误是尽管更改了 API 密钥,但原始凭据仍保留在模拟器上的应用程序中。在模拟器(或设备)上,转到设置>应用程序管理器>选择您的应用程序>单击清除数据。

    另一种解决方案是完全卸载应用程序并在重新制作凭据后重新加载。

    【讨论】:

      【解决方案2】:

      这个问题已经被问过很多次了。十次有九次是无效密钥。删除您的密钥并生成一个新密钥。

      【讨论】:

      • 如果已被多次询问,则应将其标记为重复,并将其链接到已被询问和回答的案例。
      猜你喜欢
      • 1970-01-01
      • 2015-02-13
      • 2014-02-19
      • 2018-07-16
      • 1970-01-01
      • 2013-06-02
      • 2020-04-10
      • 1970-01-01
      • 2020-05-04
      相关资源
      最近更新 更多