Adding Google maps api v2 to an existing project

12,585

Solution 1

The device must have Google play services installed for Google Maps Android v2 to run:

The API is now distributed as part of the Google Play services SDK, which you can download with the Android SDK Manager. To learn how to install the package, see Installing the Maps API SDK.

You will find these docs useful!

If your application is running v1, it's probably best to run a check to see if Google Play services is installed, and if not use the old map. I've not tested it, but check the answer here for running that check. I've also found, from here you can do this:

int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext();
if(status == ConnectionResult.SUCCESS) {
    //Success! Do what you want
}

And use the following types to determine if Google Play Services is installed on the device:

public static int isGooglePlayServicesAvailable (Context context)

Verifies that Google Play services is installed and enabled on this device, and that the version installed on this device is no older than the one required by this client.

Returns status code indicating whether there was an error. Can be one of following in ConnectionResult: SUCCESS, SERVICE_MISSING, SERVICE_VERSION_UPDATE_REQUIRED, SERVICE_DISABLED, SERVICE_INVALID.

To add the map using a fragment you will need to do something like this:

private GoogleMap map;
private MapFragment mapFragment;
private void InitMap()
{
    mapFragment = ((MapFragment)getSupportFragmentManager().findFragmentById(R.id.fragment_map));

    map = mapFragment.getMap();
    map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    map.setMyLocationEnabled(false);
}

For Google Maps Android v2 and fragments, as mentioned above, this is a great resource! Oh, and remember to use the Google Play services SDK!

Solution 2

I know this is an old question but I ran into this when I was trying to figure out how to add Google Maps to an existing Android Studio Project when my previous experience was adding the Google Maps API from the initial app creation.

In Android Studio, you can go with:

File --> New --> Google --> Google Maps Activity

Or right click your folder with all the Activities, and the above will still hold.

Let Android Studio sync and you'll be shown a generated xml file about your google map key. Copy paste the URL (it's the one that isn't indented) and then follow the instructions. You'll get a key after. Once you get your key, copy paste it to the "YOUR_KEY_HERE" string constant in the generated xml file and you're good to go.

Solution 3

You just have to change the Build Target of your project.

Under Eclipse, go to Window > Preferences > Android In the Project Build Target list, select one which provides Google APIs.

Note: this is only valid if targeting Google Maps API for Android v1. Google Maps API for Android v2 is provided by a library project

Share:
12,585
Brian Var
Author by

Brian Var

I'm a creative web developer with 5+ years’ experience in front-end development and a keen interest in developing responsive dashboards. I've constructed contact center, administration and reporting web applications using the latest JavaScript frameworks. I have good experience in performing requirements scoping.

Updated on June 19, 2022

Comments

  • Brian Var
    Brian Var almost 2 years

    I'm developing an Android App to incorporate the google maps api in a separate activity. But when I read tutorials it states the build target must be set to google api at project creation. My question is it possible to add the map to an existing project?

  • Brian Var
    Brian Var about 11 years
    Have installed the google play services and the api so my emulator is set.Would you say the simplest way to add a map is using a map fragment?
  • ingh.am
    ingh.am about 11 years
    It's not really a case of how simple it is, I will update my answer with how I do it.
  • ingh.am
    ingh.am about 11 years
    Oh, it's worth noting that you must test this on a device. It will not work on the emulator.
  • Ishita Sinha
    Ishita Sinha almost 6 years
    Just what I couldn't figure out. Thank you.