MapActivity:编程设置APIKey [英] MapActivity: set APIKey programmatically

查看:225
本文介绍了MapActivity:编程设置APIKey的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用的是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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆