You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio中GeoDataClient与PlaceDetectionClient无法解析的解决咨询

Fixing GeoDataClient & PlaceDetectionClient Not Found with New Places SDK

Hey there, let's sort out this issue! The problem here is that Google completely redesigned the Places API when they launched the new Places SDK for Android (com.google.android.libraries.places:places), and the old GeoDataClient and PlaceDetectionClient classes from the deprecated play-services-places are no longer part of the new library. Instead, you'll use the unified PlacesClient class to handle all place-related operations.

Here's a step-by-step solution:

1. Confirm Your Dependency Setup

First, make sure you only have the new Places SDK dependency in your build.gradle (remove any leftover references to com.google.android.gms:play-services-places if you had them). Your updated dependencies block should look like this:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.google.android.gms:play-services-maps:17.0.0'
    implementation 'com.google.android.gms:play-services-location:17.0.0'
    // Keep only the new Places SDK dependency
    implementation 'com.google.android.libraries.places:places:1.1.0'
}

2. Initialize the Places SDK

Before using any place features, you need to initialize the SDK with your Google Cloud API key (make sure the Places API is enabled in your Google Cloud Console!). Do this in your Application class or the onCreate() method of your main Activity:

import com.google.android.libraries.places.api.Places;
import com.google.android.libraries.places.api.net.PlacesClient;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Initialize Places SDK with your API key
    if (!Places.isInitialized()) {
        Places.initialize(getApplicationContext(), "YOUR_GOOGLE_CLOUD_API_KEY");
    }

    // Create a PlacesClient instance (replaces both old clients)
    PlacesClient placesClient = Places.createClient(this);
}

3. Replace GeoDataClient Functionality

The old GeoDataClient handled tasks like fetching place details or autocomplete predictions. Use these PlacesClient methods instead:

Example: Fetch Place Details by ID

import com.google.android.libraries.places.api.model.Place;
import com.google.android.libraries.places.api.net.FetchPlaceRequest;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.common.api.ApiException;
import java.util.Arrays;

// Request details for a specific Place ID
FetchPlaceRequest request = FetchPlaceRequest.newInstance(
        "TARGET_PLACE_ID",
        Arrays.asList(Place.Field.ID, Place.Field.NAME, Place.Field.ADDRESS, Place.Field.LAT_LNG)
);

placesClient.fetchPlace(request)
        .addOnSuccessListener(response -> {
            Place place = response.getPlace();
            // Use the retrieved place data
            Log.i("PlaceInfo", "Name: " + place.getName() + ", Address: " + place.getAddress());
        })
        .addOnFailureListener(exception -> {
            if (exception instanceof ApiException) {
                ApiException apiException = (ApiException) exception;
                Log.e("PlaceError", "Failed to fetch place: " + apiException.getStatusCode());
            }
        });

Example: Get Autocomplete Predictions

import com.google.android.libraries.places.api.net.FindAutocompletePredictionsRequest;
import com.google.android.libraries.places.api.model.AutocompletePrediction;

// Request autocomplete suggestions for a search query
FindAutocompletePredictionsRequest autoCompleteRequest = FindAutocompletePredictionsRequest.builder()
        .setQuery("Coffee shops downtown")
        .build();

placesClient.findAutocompletePredictions(autoCompleteRequest)
        .addOnSuccessListener(response -> {
            for (AutocompletePrediction prediction : response.getAutocompletePredictions()) {
                Log.i("AutoComplete", "Suggestion: " + prediction.getFullText(null));
            }
        })
        .addOnFailureListener(exception -> {
            if (exception instanceof ApiException) {
                ApiException apiException = (ApiException) exception;
                Log.e("AutoCompleteError", "Prediction failed: " + apiException.getStatusCode());
            }
        });

4. Replace PlaceDetectionClient Functionality

The old PlaceDetectionClient handled current device location place detection. Use PlacesClient.getCurrentPlace() instead:

import com.google.android.libraries.places.api.net.CurrentPlaceRequest;
import com.google.android.libraries.places.api.model.PlaceLikelihood;
import java.util.Arrays;

// Request the device's current place
List<Place.Field> placeFields = Arrays.asList(Place.Field.ID, Place.Field.NAME, Place.Field.LAT_LNG);
CurrentPlaceRequest currentPlaceRequest = CurrentPlaceRequest.newInstance(placeFields);

placesClient.getCurrentPlace(currentPlaceRequest)
        .addOnSuccessListener(response -> {
            for (PlaceLikelihood placeLikelihood : response.getPlaceLikelihoods()) {
                Place place = placeLikelihood.getPlace();
                Log.i("CurrentPlace", "Nearby place: " + place.getName() + " (Likelihood: " + placeLikelihood.getLikelihood() + ")");
            }
        })
        .addOnFailureListener(exception -> {
            if (exception instanceof ApiException) {
                ApiException apiException = (ApiException) exception;
                Log.e("CurrentPlaceError", "Failed to get current place: " + apiException.getStatusCode());
            }
        });

Don't forget to add location permissions to your AndroidManifest.xml if you're accessing the device's location:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

内容的提问来源于stack exchange,提问作者J.Dragon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 09:26:39