GoogleMapView is not loading tiles

梦想的初衷 提交于 2019-12-13 07:26:41

问题


I have simple Fragment class, specialized for displaying Google Map:

public class MapFragment extends Fragment implements Map, OnMapReadyCallback {

    private static final String TAG = MapFragment.class.getSimpleName();

    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_map, container, false);
        MapView googleMapView = (MapView) view.findViewById(R.id.mapFragment_mapView);
        googleMapView.onCreate(savedInstanceState);
        googleMapView.getMapAsync(this);

        return view;
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        Log.d(TAG, "onMapReady have called");
        MapsInitializer.initialize(this.getActivity());
    }
}
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <com.google.android.gms.maps.MapView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/mapFragment_mapView" />

</FrameLayout>

When i run my app, i can see the log message, which means i've got a valid object GoogleMap. But on the screen i see this picture:

Object GoogleMap is really valis - for example i can turn on My Location button and i will see it on screen. So whymap tiles not loading???


回答1:


You can make it work like this. This is having an additional benefit that the fragment can have other elements too displayed if needed.

public class DashBoardFragment extends Fragment{
    private MapView mMapView;
    private GoogleMap mGoogleMap;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.fragment_dash_board, container, false);

        mMapView = (MapView) view.findViewById(R.id.map_dashBoard);
        mMapView.onCreate(savedInstanceState);
        mMapView.onResume();

        try {
            MapsInitializer.initialize(getActivity().getApplicationContext());
        } catch (Exception e) {
            e.printStackTrace();
        }
        mGoogleMap = mMapView.getMap();

    return view;
    }
}

xml for the fragment goes like this

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.screens.DashBoardFragment">
    <com.google.android.gms.maps.MapView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/map_dashBoard"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</FrameLayout>

Google play services should be available in device. You can check by using this.

public static boolean isGooglePlayServiceAvailable(Activity context){
        int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(context);
        if(status==ConnectionResult.SUCCESS){
            return true;
        }else{
            GooglePlayServicesUtil.getErrorDialog(status, context, 10).show();
            return false;
        }
    }

And last but very much important , our map key should be present in Manifest

<meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="your google map key" />



回答2:


You have to call the 'getMapAsync()' function to display the map.In your onCreateView use getMapAsync() to set the callback on the fragment.

Here's a sample code for getMapAsync:

MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map); mapFragment.getMapAsync(this);

You may also try to read the Official Google Documentation for MapFragment: https://developers.google.com/android/reference/com/google/android/gms/maps/MapFragment#getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)



来源:https://stackoverflow.com/questions/34947237/googlemapview-is-not-loading-tiles

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!