为什么我不能使用我新创建的 API 密钥?
Why can't I use my newly created API key?
在开发者控制台中启用 Places API 后,我刚刚创建了一个新的 API 密钥。
实现地点选择器的代码API:
Places.initialize(getApplicationContext(), "@string/google_place_api");
List<com.google.android.libraries.places.api.model.Place.Field> placeFields = new ArrayList<>(Arrays.asList(com.google.android.libraries.places.api.model.Place.Field.values()));
List<TypeFilter> typeFilters = new ArrayList<>(Arrays.asList(TypeFilter.values()));
// Create a RectangularBounds object.
RectangularBounds bounds = RectangularBounds.newInstance(
new LatLng(-33.880490, 151.184363),
new LatLng(-33.858754, 151.229596));
Intent autocompleteIntent =
new Autocomplete.IntentBuilder(AutocompleteActivityMode.FULLSCREEN, placeFields)
.setLocationBias(bounds)
.setTypeFilter(typeFilters.get(0))
.build(getApplicationContext());
startActivityForResult(autocompleteIntent, 1001);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1001) {
if (resultCode == RESULT_OK) {
Place place = Autocomplete.getPlaceFromIntent(data);
Toast.makeText(getApplicationContext(),place.getName()+ ", " + place.getId(), Toast.LENGTH_SHORT).show();
} else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
// TODO: Handle the error.
Status status = Autocomplete.getStatusFromIntent(data);
Toast.makeText(getApplicationContext(), status.getStatusMessage(), Toast.LENGTH_SHORT).show();
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(getApplicationContext(), "Please select a location", Toast.LENGTH_SHORT).show();
}
}
}
问题是,每当我尝试搜索某个位置时,我总是收到显示 提供的 API 密钥无效 的消息。这怎么可能?我得到的 API 密钥是全新的。
编辑: 这是我在 Logcat
中不断遇到的错误
The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
2019-04-29 19:36:10.285 14975-14975/com.example.myapplication E/Places: Error while autocompleting: REQUEST_DENIED
您传递的 API 密钥不正确。
替换此行:
Places.initialize(getApplicationContext(), "@string/google_place_api");
与:
Places.initialize(getApplicationContext(), getString(R.string.google_place_api));
在开发者控制台中启用 Places API 后,我刚刚创建了一个新的 API 密钥。
实现地点选择器的代码API:
Places.initialize(getApplicationContext(), "@string/google_place_api");
List<com.google.android.libraries.places.api.model.Place.Field> placeFields = new ArrayList<>(Arrays.asList(com.google.android.libraries.places.api.model.Place.Field.values()));
List<TypeFilter> typeFilters = new ArrayList<>(Arrays.asList(TypeFilter.values()));
// Create a RectangularBounds object.
RectangularBounds bounds = RectangularBounds.newInstance(
new LatLng(-33.880490, 151.184363),
new LatLng(-33.858754, 151.229596));
Intent autocompleteIntent =
new Autocomplete.IntentBuilder(AutocompleteActivityMode.FULLSCREEN, placeFields)
.setLocationBias(bounds)
.setTypeFilter(typeFilters.get(0))
.build(getApplicationContext());
startActivityForResult(autocompleteIntent, 1001);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1001) {
if (resultCode == RESULT_OK) {
Place place = Autocomplete.getPlaceFromIntent(data);
Toast.makeText(getApplicationContext(),place.getName()+ ", " + place.getId(), Toast.LENGTH_SHORT).show();
} else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
// TODO: Handle the error.
Status status = Autocomplete.getStatusFromIntent(data);
Toast.makeText(getApplicationContext(), status.getStatusMessage(), Toast.LENGTH_SHORT).show();
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(getApplicationContext(), "Please select a location", Toast.LENGTH_SHORT).show();
}
}
}
问题是,每当我尝试搜索某个位置时,我总是收到显示 提供的 API 密钥无效 的消息。这怎么可能?我得到的 API 密钥是全新的。
编辑: 这是我在 Logcat
中不断遇到的错误The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
2019-04-29 19:36:10.285 14975-14975/com.example.myapplication E/Places: Error while autocompleting: REQUEST_DENIED
您传递的 API 密钥不正确。
替换此行:
Places.initialize(getApplicationContext(), "@string/google_place_api");
与:
Places.initialize(getApplicationContext(), getString(R.string.google_place_api));