在Android中集成google place sdk时出错,“错误:输入com.google.android.libraries.places.internal.nx”

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

我正在整合谷歌地方sdk到Android,它在编译代码时给我错误:

错误:类型com.google.android.libraries.places.internal.nx被引用为来自interfacecom.google.android.libraries.places.internal.jx$a

build.gradle

implementation 'com.google.android.libraries.places:places:1.1.0'
implementation 'com.google.android.libraries.places:places-compat:1.1.0'

MainActivity

@Override
public void onCreate() {
    super.onCreate();
    Places.initialize(this, getResources().getString(R.string.google_free_api_key));
    InternetAvailabilityChecker.init(this);
    StateSaver.setEnabledForAllActivitiesAndSupportFragments(this, true);
    appExecutors = new AppExecutors();

}

private void searchPlaces(final String query){

    // Create a new token for the autocomplete session. Pass this to FindAutocompletePredictionsRequest,
    // and once again when the user makes a selection (for example when calling fetchPlace()).
    AutocompleteSessionToken token = AutocompleteSessionToken.newInstance();

    // Create a RectangularBounds object.
    RectangularBounds bounds = RectangularBounds.newInstance(
            new LatLng(0.703579, -82.689471),
            new LatLng(-18.360459, -68.571046));
    // Use the builder to create a FindAutocompletePredictionsRequest.
    FindAutocompletePredictionsRequest request = FindAutocompletePredictionsRequest.builder()
            // Call either setLocationBias() OR setLocationRestriction().
            .setLocationBias(bounds)
            //.setLocationRestriction(bounds)
            .setCountry("pe")
            .setTypeFilter(TypeFilter.ADDRESS)
            .setSessionToken(token)
            .setQuery(query)
            .build();

    placesClient.findAutocompletePredictions(request).addOnSuccessListener((response) -> {

        List<Place.Field> placeFields = Arrays.asList(Place.Field.ID, Place.Field.NAME, Place.Field.LAT_LNG);

        viewModel.getDirections().clear();
        for (AutocompletePrediction prediction : response.getAutocompletePredictions()) {
            Log.i(LOG_TAG, prediction.getPlaceId());
            Log.i(LOG_TAG, prediction.getPrimaryText(null).toString());

            FetchPlaceRequest fetchPlaceRequest = FetchPlaceRequest.builder(prediction.getPlaceId(), placeFields)
                    .build();

            placesClient.fetchPlace(fetchPlaceRequest).addOnSuccessListener((responseFechaPlace) -> {
                Place place = responseFechaPlace.getPlace();
                Log.i(LOG_TAG, "Place found: " + place.getName());

                viewModel.getDirections()
                        .add(new GeoPunto(prediction.getPrimaryText(null).toString(), "", place.getLatLng().latitude, place.getLatLng().longitude));


            }).addOnFailureListener((exception) -> {
                if (exception instanceof ApiException) {
                    ApiException apiException = (ApiException) exception;
                    int statusCode = apiException.getStatusCode();
                    // Handle error with given status code.
                    Log.e(LOG_TAG, "Place not found: " + exception.getMessage());
                }
            });

        }

        directionsAdapter.notifyDataSetChanged();

    }).addOnFailureListener((exception) -> {
        if (exception instanceof ApiException) {
            ApiException apiException = (ApiException) exception;
            Log.e(LOG_TAG, "Place not found: " + apiException.getStatusCode());
        }
    });

}
android google-places-api google-places googleplacesautocomplete
1个回答
2
投票

这两个依赖项可能相互冲突:

// implementation "com.google.android.libraries.places:places-compat:1.1.0"
implementation "com.google.android.libraries.places:places:1.1.0"

places-compat包装器通常不需要 - 更好地将您的代码重构为新库。

© www.soinside.com 2019 - 2024. All rights reserved.