我一直收到这个错误:
找不到API密钥。检查<元数据android:name =“com.google.android.geo.API_KEY”android:value =“你的API密钥”/>在AndroidManifest.xml
的元素中当我的地图活动试图给活动的片段充气时。
有没有人遇到过这个问题?
在我的字符串文件中,我有:
<string name="google_api_key" translatable="false" templateMergeStrategy="preserve">an api key that i got from google here</string>
我试过了
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_api_key"/>
并直接在字段中输入密钥:
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="an api key that i got from google here"/>
我也尝试过:
MapActivity.java
public class MapActivity extends AppCompatActivity {
private static final String MAP_COORDS = "com.mycompany.myapp.map_coordinates";
public static final int MAP_REQUEST_CODE = 453;
private MyObject myObject;
public static Intent newIntent(Context packageContext, MyObject myObject) {
Intent intent = new Intent(packageContext, MapActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelable(MAP_COORDS, myObject);
intent.putExtras(bundle);
return intent;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
}
}
MapActivity.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.mycompany.myapp.Views.MasterDetails.MapActivity">
<fragment
android:id="@+id/map"
android:name="com.mycompany.myapp.Views.MasterDetails.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
在我的Gradle依赖项中
compile 'com.google.android.gms:play-services-maps:7.8.0'
compile 'com.google.android.gms:play-services-location:7.8.0'
的AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_api_key"/>
答案 0 :(得分:0)
首先,API_KEY应该位于“ application”标记下(如下所示),并确保您的API_KEY有效,您应该在Developer Console中启用“ Android的Places API”,而不仅仅是“ Places API” ”。
<application
android:name=...>
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_api_key" />
...
</application>
我也无法透露您的MapActivity,但是启动PlacePickerActivity的正确方法如下:
科特琳:
val builder = PlacePicker.IntentBuilder()
startActivityForResult(builder.build(this), PLACE_PICKER_REQUEST)
和Java:
PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
try {
startActivityForResult(builder.build(this), PLACE_PICKER_REQUEST);
}
catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
}
catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
希望其中一种帮助,加油!