![](/img/trans.png)
[英]Android how to set Google Map Api Key dynamically not from AndroidManifest
[英]How to give map api key dynamically in android
我的 android 應用程序中有一個用例,我的應用程序用戶必須提供 api 密鑰,以便他們可以使用 map。
但我看到我必須在清單文件中提供 api 密鑰。 我無法在運行時編輯。
有沒有其他方法可以將 map api 密鑰動態地提供給谷歌 map(我正在使用 MapView)或任何動態更改元數據值的方法
不幸的是,這似乎根本不可能。
https://developers.google.com/maps/documentation/android-sdk/get-api-key#add_key嚴格討論清單以及如何將密鑰添加到local.properties
:
打開項目級目錄中的 local.properties,然后添加以下代碼。 將 YOUR_API_KEY 替換為您的 API 密鑰。
MAPS_API_KEY=YOUR_API_KEY
然后從清單中引用它:
In your AndroidManifest.xml file, go to com.google.android.geo.API_KEY and update the android:value attribute as follows:
<meta-data android:name="com.google.android.geo.API_KEY" android:value="${MAPS_API_KEY}" />
顯然,很久以前確實有一種方法可以在手動實例化MapView
object 時注入密鑰(即不使用布局 xml): https://stackoverflow.com/a/11739039
mMapView = new MapView(this, mapApiKey);
但是,此構造函數已被刪除,因為您不能再提供 API 密鑰: https://developers.google.com/maps/documentation/android-sdk/reference/com/google/android/libraries/maps/MapView?hl= en#public-constructor-summary
MapView(Context context)
MapView(Context context, AttributeSet attrs)
MapView(Context context, AttributeSet attrs, int defStyle)
MapView(Context context, GoogleMapOptions options)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.