Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic API KEY for Google Maps on Android

Tags:

We are developing an Android application that uses google maps.

Right now, for development purposes, we're using the key like this:

<meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_key" />

And

<string name="google_maps_key">KEY_HERE</string>

For deploying purposes, we will implement a "Bring Your Own Key" approach where each client that buys the product, also put his key to be used.

I know it's weird and somewhat unnecessary, but it is a process from a big company and this decision comes from "the top".

Is there any way that we can have dynamic keys? Like putting a key into a service and consuming it in the app or something like that?

Appreciate any help.

like image 306
Renan Souza Avatar asked Jul 12 '18 17:07

Renan Souza


1 Answers

This is not possible in Google API V2 according to the documentation 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

But its possible for MapView.

If you are instantiating a MapView directly from the code, you should pass the Maps API Key in the MapView constructor.

Code:

@Override
protected void onCreate(.....) {
     super.onCreate(....);
     mMapView = new MapView(this, YOUR_MAP_API_KEY); //pass key to MapView Constructor here
     setContentView(mMapView);
  //   ....
}
like image 162
Raj Avatar answered Sep 28 '22 19:09

Raj