2013-05-10 104 views
2

我有它的后续段代码一个简单的活动:如何知道Google地图是否可以使用?

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    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" 
    android:gravity="center_horizontal" 
    android:orientation="horizontal" 
    android:id="@+id/title_complex"> 

    <fragment 
     android:id="@+id/map" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     class="com.google.android.gms.maps.MapFragment" 
     tools:context=".MainActivity" /> 

</LinearLayout> 
  • 如何MainActivity.java检查地图全负荷并且准备好了?

回答

3

在您可以与GoogleMap对象进行交互之前,您需要确认一个对象可以实例化,并且Google Play服务组件已正确安装在目标设备上。您可以验证的GoogleMap可通过调用

MapFragment.getMap()

MapView.getMap()

方法和检查返回的对象不是null。

下面显示了一个确认GoogleMap可用性的测试示例。可以从onCreate()onResume()阶段调用此方法,以确保地图始终可用。

private void setUpMapIfNeeded() { 
// Do a null check to confirm that we have not already instantiated the map. 
if (mMap == null) { 
    mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)) 
         .getMap(); 
    // Check if we were successful in obtaining the map. 
    if (mMap != null) { 
     // The Map is verified. It is now safe to manipulate the map. 

    } 
} 
} 

参考 - https://developers.google.com/maps/documentation/android/map

1
+0

你好@MaciejGórski,感谢您快速的答案,但请你提供一个简单的用法,因为我已经检查了联系,但没有一个例子。 – 2013-05-10 10:09:04

+1

@YordanYanakiev请参阅[这里](https://github.com/mg6maciej/android-maps-v2-demo/blob/master/NewMapsDemo/src/pl/mg6/newmaps/demo/GooglePlayServicesErrorDialogFragment.java)第51行。 – 2013-05-10 10:14:21

相关问题