我的 android 应用程序中有一个用例,我的应用程序用户必须提供 API key 才能使用 map 。
但我看到我必须在 list 文件中提供 API key 。我无法在运行时编辑。
是否有任何其他方法可以动态地将 map API key 提供给谷歌地图(我正在使用 MapView)或任何动态更改元数据值的方法
最佳答案
不幸的是,这似乎根本不可能。
谷歌文档
https://developers.google.com/maps/documentation/android-sdk/get-api-key#add_key严格讨论 list 以及如何将 key 添加到 local.properties
:
Open the local.properties in your project level directory, and then add the following code. Replace YOUR_API_KEY with your API key.
MAPS_API_KEY=YOUR_API_KEY
然后从 list 中引用它:
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
时注入(inject) key 。手动对象(即不使用布局 xml):https://stackoverflow.com/a/11739039mMapView = new MapView(this, mapApiKey);
但是,此构造函数已被删除,因为您无法再提供 API key :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)
关于android - 如何在android中动态地给map api key,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70540721/