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

4

6

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.

Klecka answered 11/7, 2013 at 8:33 Comment(2)
Why would you want to set it dynamically?Bernete
It was a weird requirement as they (client) wanted to sign and set the API key by themselves. I only had provide an unsigned apk.Klecka
G
5

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

Glasper answered 11/7, 2013 at 9:30 Comment(0)
C
1

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.

Canella answered 11/7, 2013 at 10:37 Comment(3)
This solution doesn't work for me as I'm not going to sign the app and I don't know the API key. However this is a useful answer. I'm going to update my question.Klecka
I would like to accept your answer in this question https://mcmap.net/q/1778354/-how-to-inject-a-maps-android-v2-api-key-into-an-application/1082344Klecka
@Klecka This solution shall work for you. If your client knows how to sign the application, they will understand how to run your script with api key as parameter.Bernete
L
0

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);
}
Leveille answered 11/7, 2013 at 8:47 Comment(2)
No such constructor exist for the com.google.android.gms.maps.MapView class.Klecka
Your code works for com.google.android.maps.MapView but not for com.google.android.gms.maps.MapView (They are two different classes!)Klecka
C
0

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.

Claudclauddetta answered 11/7, 2013 at 9:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.