IsaacCisneros
IsaacCisneros

Reputation: 1953

Is it possible to set the Android Maps API v2 key programmatically?

Is it possible to set the API v2 key programmatically instead of setting this value in the AndroidManifest file?

I was able to do this with API v1 keys but I can't find some method-constructor in the MapView to do this with the current API.

Upvotes: 6

Views: 3560

Answers (4)

MaciejGórski
MaciejGórski

Reputation: 22232

Changing the Maps API v2 key directly in APK file is possible.

Simply write a script that: unzips the APK, edits binary AndroidManifest to replace predefined value (e.g. XXXXXXX...) with given key, zips it back into .apk.

After that you can normally sign the APK.

Upvotes: 1

Ranco
Ranco

Reputation: 893

according to google's documentation:

Once you have a Maps API Key, you need to reference it from a special attribute -- android:apiKey -- in the MapView element in the XML layout. If you are instantiating a MapView directly from code, you should pass the Maps API Key in the MapView constructor.

So use mapsView's constructor passing your API key. Please review this link for further info

EDIT

here's a code snippet for your problem:

@Override
protected void onCreate(Bundle arg0) {
     super.onCreate(arg0);
     String mapApiKey = <your choice logic here>
     mMapView = new MapView(this, mapApiKey);
     setContentView(mMapView);
}

Upvotes: 0

Kirit  Vaghela
Kirit Vaghela

Reputation: 12664

Use this construct of MapView

public MapView(android.content.Context context,java.lang.String apiKey)

Parameters:

context - A MapActivity object.

apiKey - A Google Maps API Key. See Obtaining a Maps API Key for complete information.

Upvotes: 0

Emil Adz
Emil Adz

Reputation: 41099

AFAIK this is not possible in Google API V2. The documentation suggest the same thing, the API key has to be assigned using the Manifest file:

https://developers.google.com/maps/documentation/android/start#adding_the_api_key_to_your_application

Upvotes: 5

Related Questions