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
klSouthwestandklNortheastcoordinates to your needs.
内容的提问来源于stack exchange,提问作者yus ala
相关产品推荐
相关产品推荐

