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

问题描述 投票:0回答:1

我刚刚在开发人员控制台中启用Places API后,我刚刚创建了一个新的API密钥。

实施Place picker 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
java android google-places-api
1个回答
0
投票

您错误地传递了API密钥。

替换此行:

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

有:

Places.initialize(getApplicationContext(), getString(R.string.google_place_api));
© www.soinside.com 2019 - 2024. All rights reserved.