Google Maps Android API V2 check if GoogleMaps are installed on device

Solution 1:

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.

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

Solution 2:

From Google guide

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