You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xamarin中如何限制Google Maps v2 Place Picker搜索范围至指定区域

Restricting Place Picker to Kuala Lumpur Area in Google Maps v2 API

To limit your Place Picker results exclusively to Kuala Lumpur's city limits, you’ll need to define geographic bounds and apply them to your PlacePicker intent. Here’s a step-by-step guide tailored to your existing code:

1. Define Kuala Lumpur’s Geographic Bounds

First, create a LatLngBounds object that encompasses Kuala Lumpur. Use these approximate coordinates (adjust them if you need tighter/expanded coverage):

// Southwest and northeast corners defining Kuala Lumpur's general area
var klSouthwest = new LatLng(2.9776, 101.5608);
var klNortheast = new LatLng(3.2998, 101.8145);
var klBounds = new LatLngBounds(klSouthwest, klNortheast);

2. Build the PlacePicker Intent with Bounds

Modify how you launch the PlacePicker to include these bounds using the SetLatLngBounds method:

// Create the bounded PlacePicker intent
var placePickerBuilder = new PlacePicker.IntentBuilder();
placePickerBuilder.SetLatLngBounds(klBounds);
var placePickerIntent = placePickerBuilder.Build(this);

// Launch the picker (use your existing request code)
StartActivityForResult(placePickerIntent, PLACE_PICKER_REQUEST_new);

3. Keep Your Existing Result Handling

Your current code for processing the selected place works perfectly with the bounded picker—no changes needed here:

if (requestCode == PLACE_PICKER_REQUEST_new && resultCode == Result.Ok) 
{ 
    GetPlace(data); 
}

private void GetPlace(Intent data) 
{ 
    var placePick = PlacePicker.GetPlace(this, data); 
    placeChoose.Text = placePick?.AddressFormatted?.ToString(); 
}

Quick Notes:

  • Double-check that your project has valid Google Maps API credentials and required dependencies set up.
  • If you need a more precise boundary (like matching Kuala Lumpur’s official city limits), refine the klSouthwest and klNortheast coordinates to your needs.

内容的提问来源于stack exchange,提问作者yus ala

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:24:52