为什么不能使用新创建的API密钥?

时间:2019-04-29 14:14:21

标签: java android google-places-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

1 个答案:

答案 0 :(得分:0)

您错误地传递了API密钥。

替换此行:

Places.initialize(getApplicationContext(), "@string/google_place_api");

具有:

Places.initialize(getApplicationContext(), getString(R.string.google_place_api));