gpt4 book ai didi

Android Places SDK 从可能性中获取地点 ID

转载 作者:行者123 更新时间:2023-11-29 22:50:41 25 4
gpt4 key购买 nike

我正在开发一个 Android 项目,我想使用当前地点 ID。我正在使用 Google Places SDK。我可以获得可能性和名称。但我无法获得 placeID。你可以从这里看到代码。 https://developers.google.com/places/android-sdk/current-place

    // Use fields to define the data types to return.
List<Place.Field> placeFields = Collections.singletonList(Place.Field.NAME);

// Use the builder to create a FindCurrentPlaceRequest.
FindCurrentPlaceRequest request =
FindCurrentPlaceRequest.newInstance(placeFields);

// Call findCurrentPlace and handle the response (first check that the user has granted permission).
if (ContextCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
Task<FindCurrentPlaceResponse> placeResponse = placesClient.findCurrentPlace(request);
placeResponse.addOnCompleteListener(task -> {
if (task.isSuccessful()){
FindCurrentPlaceResponse response = task.getResult();
for (PlaceLikelihood placeLikelihood : response.getPlaceLikelihoods()) {


//when I try to get id it shows null.

String id = placeLikelihood.getPlace().getId();
Log.d(TAG, "place id: "+id);


Log.i(TAG, String.format("Place '%s' has likelihood: %f",
placeLikelihood.getPlace().getName(),
placeLikelihood.getLikelihood()));
}
} else {
Exception exception = task.getException();
if (exception instanceof ApiException) {
ApiException apiException = (ApiException) exception;
Log.e(TAG, "Place not found: " + apiException.getStatusCode());
}
}
});
} else {
// A local method to request required permissions;
// See https://developer.android.com/training/permissions/requesting
getLocationPermission();
}

最佳答案

好的。我想通了。我为名称、ID 和地址创建了 3 个单独的请求。

private List<Place.Field> placeFieldsID,placeFieldsName,placeFieldsAddress;
private PlacesClient placesClient;
private FindCurrentPlaceRequest requestID,requestName,requestAddress;

placeFieldsID = Collections.singletonList(Place.Field.ID);
placeFieldsName = Collections.singletonList(Place.Field.NAME);
placeFieldsAddress = Collections.singletonList(Place.Field.ADDRESS);

requestID = FindCurrentPlaceRequest.newInstance(placeFieldsID);
requestName = FindCurrentPlaceRequest.newInstance(placeFieldsName);
requestAddress = FindCurrentPlaceRequest.newInstance(placeFieldsAddress);

Task<FindCurrentPlaceResponse> placeResponseID = placesClient.findCurrentPlace(requestID);
placeResponseID.addOnCompleteListener(task -> {
if (task.isSuccessful()) {
FindCurrentPlaceResponse response = task.getResult();
for (PlaceLikelihood placeLikelihood : response.getPlaceLikelihoods()) {
id = placeLikelihood.getPlace().getId();
Log.d(TAG, "onCreate: id: " + id);

}
Task<FindCurrentPlaceResponse> placeResponseName = placesClient.findCurrentPlace(requestName);
placeResponseName.addOnCompleteListener(new OnCompleteListener<FindCurrentPlaceResponse>() {
@Override
public void onComplete(@NonNull Task<FindCurrentPlaceResponse> task) {
FindCurrentPlaceResponse response = task.getResult();

for (PlaceLikelihood placeLikelihood : response.getPlaceLikelihoods()) {
name = placeLikelihood.getPlace().getName();
Log.d(TAG, "onCreate: name: "+name);


}
Task<FindCurrentPlaceResponse> placeResponseAddress = placesClient.findCurrentPlace(requestAddress);
placeResponseAddress.addOnCompleteListener(new OnCompleteListener<FindCurrentPlaceResponse>() {
@Override
public void onComplete(@NonNull Task<FindCurrentPlaceResponse> task) {
FindCurrentPlaceResponse response = task.getResult();

for (PlaceLikelihood placeLikelihood : response.getPlaceLikelihoods()) {
address = placeLikelihood.getPlace().getAddress();
Log.d(TAG, "onCreate: address: "+address);

}

}
});

}
});
}
});

关于Android Places SDK 从可能性中获取地点 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57957223/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com