简体   繁体   English

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

[英]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? 有没有办法限制用户可以选择他/她的位置的Google Place Picker区域?

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. 我不相信有文档可以控制实际的Place Picker意图中的Place Picker ,但你可以在`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. 您可以将maxDistance更改为您希望用户从其位置选择的最大距离。 Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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