Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Google maps api v2 to an existing project

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?

like image 457
Brian Var Avatar asked Mar 06 '13 14:03

Brian Var


3 Answers

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.

like image 62
Razgriz Avatar answered Oct 14 '22 21:10

Razgriz


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!

like image 29
ingh.am Avatar answered Oct 14 '22 22:10

ingh.am


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

like image 45
nicopico Avatar answered Oct 14 '22 22:10

nicopico