Google Maps Android API V2检查是否在设备上安装了GoogleMaps

问题描述:

使用Google Maps Android API V2时,我遵循 Google Play服务设置文档,以在我的主要活动中使用以下代码进行检查以确保已安装Google Play服务:

When using Google Maps Android API V2 I'm following the Google Play Services setup documentation to make a check to ensure that Google Play Services are installed, using the following code in my main 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应用本身.

This works fine. However, I noticed some of the older Android phones I have laying around (mostly running 2.2) were missing both GooglePlayServices as well as the Google Maps app itself.

LogCat将报告此错误: Google Maps Android API:缺少Google Maps应用程序.

LogCat will report this error: Google Maps Android API: Google Maps application is missing.

问题-如何针对设备上的Google地图可用性执行与上述检查类似的检查?其次,如果用户已经安装了Google Maps,我认为需要检查以确保其安装版本与Android Maps API的V2兼容.

Question - how can I perform a similar check to the one above for the availability of Google Maps on a device? Secondly, if the user already has Google Maps installed I think the check will need to make sure their installed version is compatible with V2 of the Android Maps API.

更新 这是我的setupMapIfNeeded()方法,该方法在onCreate()的结尾处调用.我想在这里确定是否已安装Google Maps并向用户发出警报,请参见else块:

Update Here is my setupMapIfNeeded() method which is called at the end of onCreate(). This is where I think I'd want to determine if Google Maps is installed and alert the user, see the else block:

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

在进行更多戳戳和操作之后,我意识到我只需要问PackageManager是否安装了谷歌地图. IMO应该确实包含在Google Maps Android API V2开发人员指南中……将会有很多开发人员错过了这种情况,并使用户感到沮丧.

Alright after more poking and prodding I realized I just need to ask PackageManager if google maps are installed. IMO this should really be included in the Google Maps Android API V2 developers guide...there are going to be lots of devs that miss this case and have frustrated users.

这里是检查是否安装了Google Maps的方法,如果未安装,则将用户重定向到Google Maps的Play商店列表(请参见isGoogleMapsInstalled()):

Here's how to check if Google Maps are installed and re-direct the user to the Play Store listing for google maps if it's not installed (see isGoogleMapsInstalled()):

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(isGoogleMapsInstalled())
        {
            if (mMap != null) 
            {
                setUpMap();
            }
        }
        else
        {
            Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Install Google Maps");
            builder.setCancelable(false);
            builder.setPositiveButton("Install", getGoogleMapsListener());
            AlertDialog dialog = builder.create();
            dialog.show();
        }
    }
}

public boolean isGoogleMapsInstalled()
{
    try
    {
        ApplicationInfo info = getPackageManager().getApplicationInfo("com.google.android.apps.maps", 0 );
        return true;
    } 
    catch(PackageManager.NameNotFoundException e)
    {
        return false;
    }
}

public OnClickListener getGoogleMapsListener()
{
    return new OnClickListener() 
    {
        @Override
        public void onClick(DialogInterface dialog, int which) 
        {
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=com.google.android.apps.maps"));
            startActivity(intent);

            //Finish the activity so they can't circumvent the check
            finish();
        }
    };
}

我写了一篇简短的博客文章,内容如下:

I wrote up a short blog post with these details: How to check if Google Maps are installed and redirect user to the Play Store