【问题标题】:Google Maps Android API V2 check if GoogleMaps are installed on deviceGoogle Maps Android API V2 检查设备上是否安装了 GoogleMaps
【发布时间】: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);
        }
    }
}

【问题讨论】:

标签: android-mapview google-maps-android-api-2


【解决方案1】:

好吧,经过更多的戳戳和刺激,我意识到我只需要询问 PackageManager 是否安装了谷歌地图。 IMO 这确实应该包含在 Google Maps 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();
        }
    };
}

我写了一篇简短的博客文章,其中包含以下详细信息:How to check if Google Maps are installed and redirect user to the Play Store

【讨论】:

  • 干杯,没有问题!这确实应该是 APi 的一部分,或者至少有更好的文档记录。
  • 很高兴它有帮助。我完全同意它至少应该是 API 文档的一部分……为什么不提供类似于 GooglePlayServicesUtil 的地图?
  • 非常感谢,这很有帮助。
  • @DiscDev,同意你的看法。我只是想为未来的读者写它。
  • Android 11 无法再执行此操作
【解决方案2】:

From Google guide

if (mapIntent.resolveActivity(getPackageManager()) != null) {
    ...
}

【讨论】:

    【解决方案3】:

    您可以通过调用 MapFragment.getMap() 或 MapView.getMap() 方法并检查返回的对象是否不为空来验证 GoogleMap 是否可用。

    公共谷歌地图 getMap()

    谷歌地图。如果片段的视图尚未准备好,则为 Null。这 如果片段生命周期没有经过,可能会发生 onCreateView(LayoutInflater, ViewGroup, Bundle) 呢。这也可以 如果 Google Play 服务不可用,就会发生这种情况。如果谷歌播放 之后服务变得可用并且片段已经消失 通过onCreateView(LayoutInflater, ViewGroup, Bundle),调用这个 方法将再次初始化并返回 GoogleMap。

    您可以阅读verify map availability here

    【讨论】:

    • 不,这不起作用。安装了 Google Play 服务但未安装 Google 地图 (play.google.com/store/apps/…) 调用 MapFragment.getMap() 是非空的。但是,不会加载任何图块。存在缩放按钮,视图看起来像地图,但不会下载地图数据并且 LogCat 报告:E/Google Maps Android API(23235):Google Maps 应用程序丢失。我需要一种方法来检测这一点,否则用户会抱怨应用程序无法运行。用我的 setupMapIfNeeded 方法更新了问题。
    • 我刚刚尝试删除谷歌地图,地图在我的应用程序中运行良好。可能是最新的谷歌播放服务允许在没有谷歌地图的情况下使用地图。
    猜你喜欢
    • 2017-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-29
    • 1970-01-01
    • 2012-05-20
    • 2020-03-05
    • 1970-01-01
    相关资源
    最近更新 更多