How to get Flutter google Maps current location and adress in Dart Flutter (3.7.2)
and how covert latitude and longitude into adress
To get the current location of a device using Google Maps in a Flutter app, you can use the geolocator package, which provides an easy-to-use API for accessing the device's location services.
Here's an example of how to get the current location using geolocator:
First, add the geolocator package to your project by adding the following line to your pubspec.yaml file:
dependencies:
geolocator: ^7.6.2
Next, import the geolocator package:
import 'package:geolocator/geolocator.dart';
Use the getCurrentPosition method of the Geolocator class to get the device’s current location:
You can then access the latitude and longitude of the position using the latitude and longitude properties:
double latitude = position.latitude;
double longitude = position.longitude;
Here’s an example of how to get the current location and display it in a Text widget:
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Position _currentPosition;
@override
void initState() {
super.initState();
_getCurrentLocation();
}
_getCurrentLocation() async {
Position position = await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.high);
setState(() {
_currentPosition = position;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: _currentPosition != null
? Text(
"LAT: ${_currentPosition.latitude}, LNG: ${_currentPosition.longitude}")
: CircularProgressIndicator(),
),
);
}
}
This code will display the latitude and longitude of the current position in a Text widget. Note that the desiredAccuracy parameter is used to specify the desired accuracy of the location data, which can affect the time it takes to get a location fix and the battery consumption.
if you want to get current adress despite lat long
follow this code for get current Adress same library Geolocator is use for it
Use the placemarkFromCoordinates method of the Geolocator class to get the address information for the specified latitude and longitude:
List<Placemark> placemarks = await Geolocator().placemarkFromCoordinates(
latitude,
longitude,
);
You can then access the address information from the Placemark object, which contains properties such as name, thoroughfare, subThoroughfare, locality, subLocality, administrativeArea, subAdministrativeArea, postalCode, and country. Here’s an example of how to display the address information in a Text widget:
Here’s an updated version of the previous example that displays the current address
https://pub.dev/packages/geolocator
based on the user’s current location:
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Position _currentPosition;
String _currentAddress;
@override
void initState() {
super.initState();
_getCurrentLocation();
}
_getCurrentLocation() async {
Position position = await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.high);
setState(() {
_currentPosition = position;
});
_getAddressFromLatLng();
}
_getAddressFromLatLng() async {
try {
List<Placemark> placemarks = await Geolocator()
.placemarkFromCoordinates(_currentPosition.latitude, _currentPosition.longitude);
Placemark place = placemarks[0];
setState(() {
_currentAddress =
"${place.name}, ${place.thoroughfare}, ${place.subThoroughfare}, "
"${place.locality}, ${place.administrativeArea} ${place.postalCode}, ${place.country}";
});
} catch (e) {
print(e);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: _currentPosition != null
? Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
"LAT: ${_currentPosition.latitude}, LNG: ${_currentPosition.longitude}"),
SizedBox(height: 10),
Text(_currentAddress ?? ""),
],
)
: CircularProgressIndicator(),
),
);
}
}