Getting Address from Any Location using Geolocator in Flutter
Geolocation is an essential feature in many mobile applications, allowing developers to access a device's location and retrieve information about it, such as latitude, longitude, and address. In Flutter, the geolocator
package provides a straightforward way to integrate geolocation functionality into your app. In this blog post, we'll explore how to use the geolocator
package to get the address from any location in a Flutter app.
Setting up Geolocator Package
dependencies:
geolocator: ^11.0.0
After adding the dependency, run flutter pub get
in your terminal to install the package.
Using Geolocator to Get Address
Now that you have the geolocator
package added to your project, you can start using it to fetch the address from a location. Here's a step-by-step guide:
1. Import Geolocator Package
import 'package:geolocator/geolocator.dart';
2. Create Geolocator Instance
Geolocator geolocator = Geolocator();
3. Get Current Position
Position currentPosition = await geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.best,
);
4. Get Address from Coordinates
List placemarks = await geolocator.placemarkFromCoordinates(
currentPosition.latitude,
currentPosition.longitude,
);
Placemark placemark = placemarks.first;
String address =
'${placemark.street}, ${placemark.subLocality}, ${placemark.locality}, ${placemark.country}';
5. Display Address
You can now use the retrieved address in your app as needed, such as displaying it on a UI widget or storing it for future use.
Example Implementation
Here's an example implementation of how you can use the geolocator
package to get the address from the device's current location and display it in a Flutter app:
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Geolocator Example',
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State {
Geolocator geolocator = Geolocator();
Position currentPosition;
String address = '';
@override
void initState() {
super.initState();
_getCurrentLocation();
}
Future _getCurrentLocation() async {
try {
Position position = await geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.best,
);
setState(() {
currentPosition = position;
});
List placemarks = await geolocator.placemarkFromCoordinates(
currentPosition.latitude,
currentPosition.longitude,
);
Placemark placemark = placemarks.first;
setState(() {
address =
'${placemark.street}, ${placemark.subLocality}, ${placemark.locality}, ${placemark.country}';
});
} catch (e) {
print('Error: $e');
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Geolocator Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
if (currentPosition != null)
Text(
'Latitude: ${currentPosition.latitude}\n'
'Longitude: ${currentPosition.longitude}',
textAlign: TextAlign.center,
),
SizedBox(height: 20),
if (address.isNotEmpty)
Text(
'Address: $address',
textAlign: TextAlign.center,
),
],
),
),
);
}
}
Conclusion
Integrating geolocation features into your Flutter app using the geolocator
package is straightforward and enables you to access essential information about the device's location. Whether you're building location-based services, mapping applications, or simply need to fetch address details, the geolocator
package provides the necessary tools to accomplish these tasks efficiently. Experiment with the provided code and explore the possibilities of incorporating geolocation functionalities into your Flutter projects!
Comments
Post a Comment