A Flutter package Mapbox Geocoding API. Package is not official product of mapbox.
This project is a library to use Mapbox Geocoding API from dart
scripts.
Library documentation is here.
To use this library in your code:
-
add a dependency in your
pubspec.yaml
:dependencies: mapbox_geocoding: "^last_version"
-
add import in your
dart
code:import 'package:mapbox_geocoding/mapbox_geocoding.dart';
Please create/use Mapbox account to obtain Mapbox access token.
If you have Mapbox account, you can reach access token from Mapbox account dashboard.
A very simple example :
import 'package:mapbox_geocoding/mapbox_geocoding.dart';
import 'package:mapbox_geocoding/model/forward_geocoding.dart';
MapboxGeocoding geocoding = MapboxGeocoding('YOUR_ACCESS_TOKEN');
///Forward geocoding. Get latitude and longitude from place name.
getCoordinates(String city) async {
try {
ForwardGeocoding forwardModel = await geocoding.forwardModel(city);
return forwardModel.features[0].center;
} catch (Excepetion) {
return 'Forward Geocoding Error';
}
}
///Reverse geocoding. Get place name from latitude and longitude.
getCity(double lat, double lng, String apiKey) async {
try {
ReverseGeocoding reverseModel = await geocoding.reverseModel(lat, lng);
return reverseModel.features[0].placeName;
} catch (Excepetion) {
return 'Reverse Geocoding Error';
}
}