MapActivity:编程设置APIKey [英] MapActivity: set APIKey programmatically
问题描述
我目前使用的是MapActivity在我的应用程序。 我用它与2 API密钥。一个用于调试,一个用于生产
I currently use a MapActivity in my application. I use it with 2 API Keys. One for debugging, and one for "production"
我厌倦了在XML布局改变这些值:
I am fed up with changing these values in the xml layout:
<view class="com.google.android.maps.MapView"
android:id="@+id/myGmap"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled="true"
android:clickable="true"
android:apiKey="@string/api_key_prod" />
我厌倦了试图每次改变apikey并通过每一次调试更换刺。
I am fed up trying to change the apikey each time and replace prod by debug each time.
这是可能的的onCreate内修改应用中这个键()。
Is that possible to change this key within the onCreate() of my application.
想象一下,我有一个布尔值preference看起来像:isDebug
Imagine that I have a boolean preference that look like: isDebug.
我可以检查THI preference我的手机上,并在默认情况下对用户应用程序禁用它。做出这样的:
I may check thi preference on my phone and disable it by default on user application. and make something like:
if (isDebug)
myMap.setApiKey(R.string.api_key_debug)
else
myMap.setApiKey(R.string.api_key_prod)
感谢很多的任何帮助。
推荐答案
这对我的作品。
MapView类的构造函数的这一版本记录在这里: <一href="https://developers.google.com/maps/documentation/android/reference/com/google/android/maps/MapView">https://developers.google.com/maps/documentation/android/reference/com/google/android/maps/MapView
This variant of MapView constructor is documented here: https://developers.google.com/maps/documentation/android/reference/com/google/android/maps/MapView
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
String mapApiKey = <your choice logic here>
mMapView = new MapView(this, mapApiKey);
setContentView(mMapView);
这篇关于MapActivity:编程设置APIKey的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!