gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 09:04:37 25 4
gpt4 key购买 nike

使用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 map 可用性方面的上述检查?其次,如果用户已经安装了Google Maps,我认为需要进行检查以确保其安装版本与Android Maps API的V2兼容。

更新
这是我的setupMapIfNeeded()方法,该方法在onCreate()的结尾处调用。我认为这是我要确定是否已安装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);
}
}
}

最佳答案

好了,经过更多的戳戳之后,我意识到我只需要问PackageManager是否安装了Google map 。 IMO应该确实包含在Google Maps Android API V2开发人员指南中...将会有很多开发人员错过了这种情况,并且使用户感到沮丧。

以下是检查是否安装了Google Maps并将用户重定向到Google Maps的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

关于android-mapview - Google Maps Android API V2检查设备上是否安装了GoogleMaps,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13778965/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com