Android上的Google Place Picker - 限制区域/半径

时间:2016-05-19 19:08:06

标签: android google-places-api

有没有办法限制用户可以选择他/她的位置的Google Place Picker区域?

来自当前位置的半径。

1 个答案:

答案 0 :(得分:1)

我不相信有文档来控制实际Place Picker意图中的选择,但你可以在`onActivityResult中完成。这是一个例子:

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();
        }
    }
}

您可以将maxDistance更改为您希望用户从其所在位置选择的最大距离。希望它有所帮助!