【问题标题】:Google Place Picker on Android - limit area/radiusAndroid 上的 Google Place Picker - 限制区域/半径
【发布时间】:2016-05-19 19:08:05
【问题描述】:

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

类似于当前位置的半径。

【问题讨论】:

标签: android google-places-api


【解决方案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 更改为您希望用户的选择与其所在位置之间的最大距离。希望对您有所帮助!

【讨论】:

    猜你喜欢
    • 2018-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多