android - 如何在android中动态地给map api key

标签 android google-maps android-mapview android-maps-v2

我的 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/11739039
mMapView = 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/

相关文章:

Android TableRow - 如何将 View 动态添加到特定位置?

android - 切换到 API21 AppCompat 后缺少 ActionBar

android - 如何在 android 中创建不同屏幕尺寸的图像

javascript - Google map - 路线服务 api ZERO_RESULTS

Android:后台服务中的计时器

javascript - 谷歌地图 API : Load specific version

android - 单击按钮时在 map 上平移坐标 (Android)

android - 使用 Json 在 Google map Mapview 中绘制路径

android - 使用 osmdroid mapview 将自定义图像用作 maptile

android - 如何根据标记位置设置 Google map 的缩放级别