Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 898
Michał Tajchert Avatar asked Nov 09 '22 14:11

Michał Tajchert


1 Answers

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!

like image 134
AkashBhave Avatar answered Nov 15 '22 06:11

AkashBhave