Mapbox Autocomplete Place Search
A Flutter package for Mapbox place search with autocomplete functionality, featuring flexible JSON parsing, comprehensive error handling, and easy-to-use widgets for place search and selection.
Features
✨ Easy-to-use widgets - Drop-in place search fields and dialogs
🔍 Autocomplete search - Real-time search suggestions as you type
🌍 Flexible filtering - Filter by place types, countries, and proximity
🛡️ Robust error handling - Comprehensive error handling and logging
📱 Mobile optimized - Built specifically for Flutter mobile apps
🔧 Customizable - Extensive configuration options for different use cases
Getting started
Prerequisites
- Mapbox Account: Sign up at Mapbox to get an access token
- Flutter SDK: Ensure you have Flutter 3.0.0 or higher installed
Installation
Add this to your package's pubspec.yaml file:
dependencies:
mapbox_autocomplete_place_search: ^1.0.0
Setup
- Get your Mapbox access token from Mapbox Account
- Use a PUBLIC token (pk.) - not a secret key (sk.) for client-side operations
- Configure your token in your app
Usage
Basic Place Search Field
import 'package:mapbox_autocomplete_place_search/mapbox_autocomplete_place_search.dart';
PlaceSearchField(
accessToken: 'your_mapbox_access_token_here',
hintText: 'Search for a place...',
maxResults: 5,
onPlaceSelected: (place) {
print('Selected: ${place.text} at ${place.latitude}, ${place.longitude}');
},
)
Place Search Dialog
showDialog(
context: context,
builder: (context) => PlaceSearchDialog(
accessToken: 'your_mapbox_access_token_here',
title: 'Search for Places',
hintText: 'Enter place name...',
onPlaceSelected: (place) {
print('Selected: ${place.text}');
Navigator.of(context).pop();
},
),
);
Advanced Search with Filtering
final geocodingService = MapboxGeocodingService(
accessToken: 'your_mapbox_access_token_here',
);
final results = await geocodingService.searchPlaces(
query: 'New York',
limit: 10,
types: ['place', 'locality', 'neighborhood'],
country: ['us'],
proximity: [-74.006, 40.7128], // NYC coordinates
);
Available Place Types
country- Countriesregion- States/provincespostcode- ZIP codesdistrict- Districtsplace- Cities/townslocality- Localitiesneighborhood- Neighborhoodsaddress- Street addressespoi- Points of interest
API Reference
PlaceSearchField
A text field with built-in autocomplete place search.
Properties:
accessToken- Your Mapbox access token (required)hintText- Placeholder text for the search fieldmaxResults- Maximum number of search results to showonPlaceSelected- Callback when a place is selected
PlaceSearchDialog
A full-screen dialog for place search.
Properties:
accessToken- Your Mapbox access token (required)title- Dialog titlehintText- Placeholder text for the search fieldonPlaceSelected- Callback when a place is selected
MapboxGeocodingService
Direct API service for custom implementations.
Methods:
searchPlaces()- Search for places by queryreverseGeocode()- Get place details from coordinatesgetPlaceDetails()- Get details for a specific place ID
Place Model
Represents a place returned from the API.
Properties:
id- Unique identifiertext- Display textplaceName- Full place namelatitude/longitude- CoordinatesplaceType- Type of placerelevance- Search relevance score
Error Handling
The package includes comprehensive error handling:
- API Errors: Proper HTTP status code handling
- Parsing Errors: Robust JSON parsing with fallbacks
- Network Errors: Graceful handling of connection issues
- Debug Logging: Extensive logging for troubleshooting
Debug Logging
Enable detailed logging to troubleshoot issues:
// Console output shows:
// 🔍 Starting place search
// 📡 API request details
// ✅ Response received
// 📍 Results found
// ❌ Any errors that occur
Example
See the /example folder for a complete working example with multiple search implementations.
Additional information
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Issues
If you encounter any issues, please file them at the GitHub repository.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Dependencies
http- For API calls to Mapboxjson_annotation- For JSON serializationjson_serializable- For code generation
Mapbox API
This package uses the Mapbox Geocoding API. Please refer to their documentation for API limits and usage guidelines.