我正在开发一个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();
}
答案 0 :(得分:1)
好的。我知道了。我为名称,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);
}
}
});
}
});
}
});