从用户点击地图的字符串地址中获取一个放置对象

时间:2019-07-06 14:11:23

标签: java android google-maps google-maps-markers google-places-api

某些背景: 我有一张地图,允许用户单击并选择一个地方。然后我从他们点击的地方得到地址。

我要做什么::使用该地址(String),我要使用获取有关该地点的详细信息 Google Map Places API。

我正在尝试使用下面的代码

//Make the Autocomplete request builder with the String address
FindAutocompletePredictionsRequest.Builder requestBuilder =
                    FindAutocompletePredictionsRequest.builder()
                            .setQuery(address)
                            .setTypeFilter(TypeFilter.ADDRESS);
placesClient = Places.createClient(getApplicationContext());

//Create the Autocomplete Task
Task<FindAutocompletePredictionsResponse> task =
                    placesClient.findAutocompletePredictions(requestBuilder.build());

task.addOnSuccessListener(new OnSuccessListener<FindAutocompletePredictionsResponse>() {
    @Override
    public void onSuccess(final FindAutocompletePredictionsResponse findAutocompletePredictionsResponse) {
        //Use the response to get a list of Predictions
        List<AutocompletePrediction> list = findAutocompletePredictionsResponse.getAutocompletePredictions();
            if (list.isEmpty() || list == null) {

            } else {
                //Get the first Autocomplete prediction and later, it's ID.
                AutocompletePrediction ap = list.get(0);
                //Provide a list of Place Fields that I want to retrieve.         
                List<Place.Field> placeFields = Arrays.asList(Place.Field.ID, Place.Field.NAME, Place.Field.ADDRESS);

                //Make the request to fetch the place using the Place ID obtained from the Autocomplete prediction.
                FetchPlaceRequest request = FetchPlaceRequest.builder(ap.getPlaceId(), placeFields).build();
                placesClient = Places.createClient(getApplicationContext());

                //Using the Place ID from the Autocomplete prediction, make a request to fetch places.
                placesClient.fetchPlace(request).addOnSuccessListener(new OnSuccessListener<FetchPlaceResponse>() {
                    @Override
                    public void onSuccess(FetchPlaceResponse fetchPlaceResponse) {
                        //Use the response to get the Place Object
                        Place place = fetchPlaceResponse.getPlace();
                        //Display the results from the Place Object
                        displayResults("\n-----------------\nName: " + place.getName() + "\nAddress: " + place.getAddress());
                    }
                }).addOnFailureListener(new OnFailureListener() {
                    //......
                });
            }
       }
});
task.addOnFailureListener(new OnFailureListener() {
    //......
});

问题:问题是,当我执行此操作并使用place.getName()时,并没有像应该显示的那样显示位置名称,而是显示了地址的第一部分,例如街道数字和名称。此外,其他字段(例如电话号码和营业时间)也为空。

有人知道我如何从Place地址获得String对象吗? 获取有关用户点击位置的详细信息是我的Android应用程序最重要的功能。

我正在使用新的Places API,因为Place DetailsPlace Picker现在已弃用。

0 个答案:

没有答案