Waz*_*_Be 21 android google-maps key api-key
我目前在我的应用程序中使用MapActivity.我使用2个API密钥.一个用于调试,一个用于"生产"
我厌倦了在xml布局中更改这些值:
<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" />
Run Code Online (Sandbox Code Playgroud)
我厌倦了每次尝试更改apikey并且每次都通过调试替换prod.
是否可以在我的应用程序的onCreate()中更改此键.
想象一下,我有一个布尔首选项,如:isDebug.
我可以检查我的手机上的首选项,并默认在用户应用程序上禁用它.做一些像:
if (isDebug)
myMap.setApiKey(R.string.api_key_debug)
else
myMap.setApiKey(R.string.api_key_prod)
Run Code Online (Sandbox Code Playgroud)
非常感谢任何帮助.
您不能在布局中都拥有窗口小部件并在Java中设置API密钥.
如果动态创建MapViewvia构造函数,则可以从Java代码中提供API密钥,但是您需要将其动态添加到布局中.
话虽这么说,我会通过你的构建过程处理这个问题(例如,基于调试/生产构建,将正确的XML文件复制到正确的目录中).
小智 5
这适合我.
此处记录了此MapView构造函数的变体: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);
Run Code Online (Sandbox Code Playgroud)