Check if Google Maps are installed on Android if not, direct user to the Play Store

The Google Maps Android API V2 developers guide is missing an important step. Beyond checking for the availability of Google Play Services on an Android device, you must also make sure the user has the Google Maps app installed. If the user has Google Play Services installed but not Google Maps, your app will load but no map data will download. The map will have zoom buttons and look like a map, but there will never be any content and you’ll notice an error message in LogCat similar to

E/Google Maps Android API(23235): Google Maps application is missing.

Here is example code of how to check if Google Maps are installed and direct the user to the Play Store listing for Google Maps if they are not installed. My setupMapIfNeeded() method is called at the end of onCreate().

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) 
            {
                mMap.setLocationSource(this);

                mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(44.9800, -93.2636), 10.0f));
                setUpMap();
            }
        }
        else
        {
            Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Please 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();
        }
    };
}
About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s