GoogleMapView没有加载瓷砖

问题描述:

我有简单的片段类,专门用于显示谷歌地图:GoogleMapView没有加载瓷砖

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> 

当我跑我的应用程序,我可以看到日志消息,这意味着我已经得到了一个有效的GoogleMap对象。但在屏幕上我看到这张图片: enter image description here

对象谷歌地图是真正的价值 - 例如我可以打开我的位置按钮,我会在屏幕上看到它。那么为什么地图瓷砖不加载?

+0

你还没有谷歌多数民众赞成签署你不能让谷歌地图或sha1注册谷歌和签署sha1也放在谷歌地图。地图将加载 – Amitsharma

你可以让它像这样工作。这还有一个额外的好处,即如果需要,片段也可以显示其他元素。

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对于该片段是这样的

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

谷歌播放服务应该在设备可用。你可以用这个来检查。

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

最后也是非常重要的,我们的地图的关键应该是出现在清单

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

感谢您的回答,我想我忘了为mapView对象添加onResume方法。 我已经有了所有的API密钥,谷歌地图已经通过SupportMapFragment在我的应用程序中工作 - 我使用MapView方式非常麻烦。 –

你必须调用“getMapAsync()”函数来显示地图。在您的onCreateView中使用getMapAsync()来设置片段上的回调。

下面是getMapAsync一个示例代码:

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

您也可以尝试读取MapFragment谷歌官方文档: https://developers.google.com/android/reference/com/google/android/gms/maps/MapFragment#getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)