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

Dis*_*Dev 29 android-mapview google-maps-android-api-2

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

@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);
  }
Run Code Online (Sandbox Code Playgroud)

这很好用.但是,我注意到我已经存在的一些较旧的Android手机(大多数运行2.2)都缺少GooglePlayServices以及Google Maps应用程序本身.

LogCat将报告此错误:Google Maps Android API:Google地图应用程序丢失.

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

更新 这是我的setupMapIfNeeded()方法,它在onCreate()的末尾被调用.这是我认为我想确定是否已安装Google地图并提醒用户的地方,请参阅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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Dis*_*Dev 54

好吧,经过更多的戳戳和刺激,我意识到我只需要问PackageManager是否安装了谷歌地图.IMO这应该真的包含在谷歌地图Android API V2开发人员指南中...会有很多开发人员错过这种情况并让用户感到沮丧.

以下是检查Google地图是否已安装的方法,并将用户重定向到Google地图的Play商品列表(如果未安装)(请参阅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();
        }
    };
}
Run Code Online (Sandbox Code Playgroud)

我写了一篇简短的博文,其中包含以下详细信息:如何检查是否安装了Google地图并将用户重定向到Play商店

  • Android 11 无法再执行此操作 (3认同)