简体   繁体   中英

Google Place Picker on Android - limit area/radius

Is there a way to limit Google Place Picker area that user is able to pick his/her place?

Something like radius from current location.

I don't believe there is documentation to control the selection in the actual Place Picker intent, but you could do it in the `onActivityResult. Here is an example:

int PLACE_PICKER_REQUEST = 1;
String toastMsg;
Location selectedLocation = new Location(provider);
Location currentLocation = new Location(provider);
double maxDistance = 10;
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == PLACE_PICKER_REQUEST) {
        if (resultCode == RESULT_OK) {
            Place place = PlacePicker.getPlace(data, this);

            selectedLocation.setLatitude(place.getLatLng().latitude);
            selectedLocation.setLongitude(place.getLatLng().longitude);
            if(selectedLocation.distanceTo(currentLocation) > maxDistance) {
                toastMsg = "Invalid selection, please reselect your place";
                // Runs the selector again
                PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
                startActivityForResult(builder.build(this), PLACE_PICKER_REQUEST);
            } else {
                // Result is fine
                toastMsg = String.format("Place: %s", place.getName());
            }
            Toast.makeText(this, toastMsg, Toast.LENGTH_LONG).show();
        }
    }
}

You can change maxDistance to the maximum distance you want the user's selection to be from their location. Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM