【发布时间】:2012-11-26 13:38:49
【问题描述】:
在使用 Google Maps Android API V2 时,我正在关注 Google Play Services setup documentation 进行检查以确保已安装 Google Play 服务,在我的主 Activity 中使用以下代码:
@Override
public void onResume()
{
checkGooglePlayServicesAvailability();
super.onResume();
}
public void checkGooglePlayServicesAvailability()
{
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if(resultCode != ConnectionResult.SUCCESS)
{
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(resultCode, this, 69);
dialog.setCancelable(false);
dialog.setOnDismissListener(getOnDismissListener());
dialog.show();
}
Log.d("GooglePlayServicesUtil Check", "Result is: " + resultCode);
}
这很好用。但是,我注意到我使用的一些较旧的 Android 手机(主要运行 2.2)缺少 GooglePlayServices 以及 Google Maps 应用程序本身。
LogCat 会报这个错误: Google Maps Android API:缺少 Google Maps 应用程序。
问题 - 如何对设备上的 Google 地图可用性执行与上述类似的检查?其次,如果用户已经安装了 Google Maps,我认为检查需要确保他们安装的版本与 Android Maps API V2 兼容。
更新 这是我在 onCreate() 结束时调用的 setupMapIfNeeded() 方法。这是我想确定是否安装了 Google Maps 并提醒用户的地方,请参阅 else 块:
private void setUpMapIfNeeded()
{
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null)
{
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.basicMap)).getMap();
if (mMap != null)
{
mMap.setLocationSource(this);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(44.9800, -93.2636), 10.0f));
setUpMap();
}
else
{
//THIS CODE NEVER EXECUTES - mMap is non-null even when Google Maps are not installed
MapConstants.showOkDialogWithText(this, R.string.installGoogleMaps);
}
}
}
【问题讨论】:
-
我能问一下你在 getErrorDialog 方法参数上有什么 69 吗?
-
@luiscvalmeida 69 只是请求代码(我选择了它)。查看developer.android.com/reference/com/google/android/gms/common/…中的getErrorDialog方法@
标签: android-mapview google-maps-android-api-2