Can I programmatically install the Android Maps API v2 key?

Can I programmatically set the API v2 key instead of setting this value in the AndroidManifest file?

I was able to do this using API v1 keys, but I cannot find any method constructor in MapView for this with the current API.

+4
source share
4 answers

AFAIK is not possible in Google API V2. The documentation offers the same thing, the API key must be assigned using the manifest file:

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

+3
source

It is possible to change the Maps API key directly in the APK file.

Just write a script that: decompresses the APK, edits the binary AndroidManifest to replace the predefined value (e.g. XXXXXXX ...) with the given key, fastens it back to .apk.

After that, you can usually sign the APK.

+1
source

according to google documentation:

After you have the Maps API key, you need to refer to it from a special attribute - android: apiKey - in the MapView element in the XML layout. If you create a MapView directly from the code, you must pass the Map API key in the MapView constructor .

Therefore, use the mapsView constructor, passing your API key. Please view this link for more information.

EDIT

here is 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); } 
0
source

Use this MapView construct

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

Options:

context is a MapActivity object.

apiKey - Google Maps API Key. See "Obtaining a Maps API Key" for complete details.

0
source

Source: https://habr.com/ru/post/1490858/


All Articles