在android中验证PlaceAutocompleteFragment

时间:2018-01-17 22:46:30

标签: android google-places-api google-maps-android-api-2

我正在尝试在android中验证PlaceAutocompleteFragment。我想添加一个toast如果它是空的,如果它不是空的那么我想做一些处理。这是我的PlaceAutocompleteFragment。如果没有选择Place,我可以附加一个事件监听器来验证吗?

仅供参考,这是适用于Android的Google Places API

final PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)
                    getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);

autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
    @Override
    public void onPlaceSelected(Place place) {
        // TODO: Get info about the selected place.           
    }

    @Override
    public void onError(Status status) {
        // TODO: Handle the error.
        Log.i("An error occured: " , status.toString());
    }
});

3 个答案:

答案 0 :(得分:0)

你也可以使用地方自动填充请求来代替使用像这样的片段

https://clips.twitch.tv/RealIgnorantHeronVoteYea

你可以在活动结果方法上得到这样的结果...

 public void openPlaceAutoComplete() {
    try {
        Intent intent =
                new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_FULLSCREEN)
                        .build(getActivity());
        startActivityForResult(intent, Constants.PLACE_AUTOCOMPLETE_REQUEST_CODE);
    } catch (GooglePlayServicesRepairableException | GooglePlayServicesNotAvailableException e) {
        // TODO: Handle the error.
        e.printStackTrace();
    }
}

答案 1 :(得分:0)

全局创建一个latLng变量。

LatLng latLngPickup = null

@Override
    public void onPlaceSelected(Place place) {
        // TODO: Get info about the selected place. 
        //Place place = places.get(0);
        latLngPickup = place.getLatLng();

    }

每当您想检查用户是否选择了该地点时,请按照

进行检查
if (latLngPickup == null) {
        // your alert \\
        return;
    }

答案 2 :(得分:0)

Sorry I had to add my code as an answer, I cant add a comment in reply to yours Rahul

Here is my code

autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
             LatLng latLngPickup = null;
                @Override
                public void onPlaceSelected(Place place) {  
                    latLngPickup=place.getLatLng();
                  }
                if(latLngPickup.latitude == null) {
                    Toast error = Toast.makeText(getApplicationContext(), "Please Select a location", Toast.LENGTH_LONG);
                    error.show();
                    return;
                    }

                    @Override
                    public void onError(Status status) {
                        // TODO: Handle the error.
                        Log.i("An error occured: " , status.toString());
                    }
                });