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 并每次都通过调试替换 prod.
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.
想象一下,我有一个如下所示的布尔首选项:isDebug.
Imagine that I have a boolean preference that look like: isDebug.
我可以在我的手机上检查这个偏好,并在用户应用程序上默认禁用它.并制作类似的东西:
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 构造函数的此变体: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屋!