2016-01-22 100 views
0

我有簡單的片段類,專門用於顯示谷歌地圖: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

回答

1

你可以讓它像這樣工作。這還有一個額外的好處,即如果需要,片段也可以顯示其他元素。

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方式非常麻煩。 –

0

你必須調用「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)