MapActivity:编程设置APIKeyMapActivity、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 "...

我目前使用的是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类的构造函数的这一版本记录在这里: 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);
阅读全文

相关推荐

最新文章