A Flutter Geocoding plugin which provides easy geocoding and reverse-geocoding features.
Important:
PlatformException(IO_ERROR, ...)
gets thrown, most of the times it means that the rate limit has been reached.To use this plugin, please follow the installation guide on the official geocoding plugin page.
NOTE: This plugin relies on the AndroidX version of the Android Support Libraries. This means you need to make sure your Android project is also upgraded to support AndroidX. Detailed instructions can be found here.
The TL;DR version is:
- Add the following to your "gradle.properties" file:
android.useAndroidX=true android.enableJetifier=true
2. Make sure you set the
compileSdkVersion
in your "android/app/build.gradle" file to 33:android { compileSdkVersion 33 ... }
3. Make sure you replace all the
android.
dependencies to their AndroidX counterparts (a full list can be found Android migration guide).
To translate an address into latitude and longitude coordinates you can use the placemarkFromAddress
method:
import 'package:geocoding/geocoding.dart';
List<Location> locations = await locationFromAddress("Gronausestraat 710, Enschede");
If you want to translate latitude and longitude coordinates into an address you can use the placemarkFromCoordinates
method:
import 'package:geocoding/geocoding.dart';
List<Placemark> placemarks = await placemarkFromCoordinates(52.2165157, 6.9437819);
Both the locationFromAddress
and placemarkFromCoordinates
accept an optional localeIdentifier
parameter. This parameter can be used to enforce the results to be formatted (and translated) according to the specified locale. The localeIdentifier
should be formatted using the syntax: [languageCode]_[countryCode]. Use the ISO 639-1 or ISO 639-2 standard for the language code and the 2 letter ISO 3166-1 standard for the country code. Some examples are:
Locale identifier | Description |
---|---|
en | All English speakers (will translate all attributes to English) |
en_US | English speakers in the United States of America |
en_UK | English speakers in the United Kingdom |
nl_NL | Dutch speakers in The Netherlands |
nl_BE | Dutch speakers in Belgium |
Please file any issues, bugs or feature requests as an issue on our GitHub page. Commercial support is available, you can contact us at hello@baseflow.com.
If you would like to contribute to the plugin (e.g. by improving the documentation, solving a bug or adding a cool new feature), please carefully review our contribution guide and send us your pull request.
Run this command:
With Flutter:
$ flutter pub add geocoding
This will add a line like this to your package's pubspec.yaml (and run an implicit flutter pub get
):
dependencies:
geocoding: ^2.1.1
Alternatively, your editor might support flutter pub get
. Check the docs for your editor to learn more.
Now in your Dart code, you can use:
import 'package:geocoding/geocoding.dart';
Example/lib/main.dart
import 'package:baseflow_plugin_template/baseflow_plugin_template.dart';
import 'package:flutter/material.dart';
import 'package:geocoding/geocoding.dart';
void main() {
runApp(_GeocodingExample());
}
/// Example [Widget] showing the use of the Geocode plugin
class GeocodeWidget extends StatefulWidget {
@override
_GeocodeWidgetState createState() => _GeocodeWidgetState();
}
class _GeocodeWidgetState extends State<GeocodeWidget> {
final TextEditingController _addressController = TextEditingController();
final TextEditingController _latitudeController = TextEditingController();
final TextEditingController _longitudeController = TextEditingController();
String _output = '';
@override
void initState() {
_addressController.text = 'Gronausestraat 710, Enschede';
_latitudeController.text = '52.2165157';
_longitudeController.text = '6.9437819';
super.initState();
}
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(24.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
const Padding(
padding: EdgeInsets.only(top: 32),
),
Row(
children: <Widget>[
Expanded(
child: TextField(
autocorrect: false,
controller: _latitudeController,
style: Theme.of(context).textTheme.bodyMedium,
decoration: InputDecoration(
hintText: 'Latitude',
),
keyboardType: TextInputType.number,
),
),
SizedBox(
width: 20,
),
Expanded(
child: TextField(
autocorrect: false,
controller: _longitudeController,
style: Theme.of(context).textTheme.bodyMedium,
decoration: InputDecoration(
hintText: 'Longitude',
),
keyboardType: TextInputType.number,
),
),
],
),
const Padding(
padding: EdgeInsets.only(top: 8),
),
Center(
child: ElevatedButton(
child: Text('Look up address'),
onPressed: () {
final latitude = double.parse(_latitudeController.text);
final longitude = double.parse(_longitudeController.text);
placemarkFromCoordinates(latitude, longitude)
.then((placemarks) {
var output = 'No results found.';
if (placemarks.isNotEmpty) {
output = placemarks[0].toString();
}
setState(() {
_output = output;
});
});
}),
),
const Padding(
padding: EdgeInsets.only(top: 32),
),
TextField(
autocorrect: false,
controller: _addressController,
style: Theme.of(context).textTheme.bodyMedium,
decoration: InputDecoration(
hintText: 'Address',
),
keyboardType: TextInputType.text,
),
const Padding(
padding: EdgeInsets.only(top: 8),
),
Center(
child: ElevatedButton(
child: Text('Look up location'),
onPressed: () {
locationFromAddress(_addressController.text)
.then((locations) {
var output = 'No results found.';
if (locations.isNotEmpty) {
output = locations[0].toString();
}
setState(() {
_output = output;
});
});
}),
),
Expanded(
child: SingleChildScrollView(
child: Container(
width: MediaQuery.of(context).size.width,
child: Text(_output),
),
),
)
],
),
);
}
}
class _GeocodingExample extends StatelessWidget {
const _GeocodingExample({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return BaseflowPluginExample(
pluginName: 'Geolocator',
githubURL: 'https://github.com/Baseflow/flutter-geolocator',
pubDevURL: 'https://pub.dev/packages/geolocator',
pages: [
ExamplePage(
Icons.pin_drop,
(BuildContext context) => GeocodeWidget(),
),
]);
}
}
View on GitHub: https://github.com/baseflow/flutter-geocoding/tree/main/geocoding