Geolocator Flutter With Example
In this article, we are going to learn how to get current location of device using geolocator flutter plugin. We will get latitude and longitude through geolocator plugin in this example. We will also get full address of current location using geocoding flutter plugin.
Nowadays get user current location is mandatory task to provide various location related functionalities. In this example, we will get location of user device using small amount of code. Geolocator plugin gives us latitude and longitude of mobile device.
Geocoding plugin gives us detailed information about current location address of user device such as street, postal code, area, locality, sublocality etc. First we need to add both required plugins in our pubspec.yaml file.
Add required dependencies in your pubspec.yaml file
geocoding: ^2.0.0
iOS Requirements for Geolocator Flutter
To get current location of user iOS device, we need to add following permissions in ios/Runner/Info.plist file.
<string>This app needs access to location when open.</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>This app needs access to location when in the background.</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>This app needs access to location when open and in the background.</string>
Android Requirements for Geolocator Flutter
To get current location of user android device, we need to add following permissions in android/app/src/main/AndroidManifest.xml.
<uses-permission android:name=”android.permission.ACCESS_COARSE_LOCATION” />
Get Latitude and Longitude
We can get current latitude and longitude of mobile device using geolocator flutter plugin as below.
Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.best,
forceAndroidLocationManager: true)
.then((Position position) {
setState(() {
currentPosition = position;
getCurrentLocationAddress();
});
}).catchError((e) {
print(e);
});
}
Get Current Location Address
We can get detailed information about user’s current location using geocoding flutter plugin as below.
try {
List<Placemark> listPlaceMarks = await placemarkFromCoordinates(
currentPosition.latitude, currentPosition.longitude);
Placemark place = listPlaceMarks[0];
setState(() {
currentLocationAddress = “${place.locality}, ${place.postalCode}, ${place.country}”;
}
);
} catch (e) {
print(e);
}
}
Geolocator Flutter UI
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
if (currentPosition != null) Text(“LAT: ${currentPosition.latitude}”, style: TextStyle(fontSize: 23),),
SizedBox(height: 15),
if (currentPosition != null) Text(“LONG: ${currentPosition.longitude}”, style: TextStyle(fontSize: 23),),
SizedBox(height: 15),
if (currentLocationAddress != null) Text(currentLocationAddress, style: TextStyle(fontSize: 23),),
SizedBox(height: 25),
ElevatedButton(
child: Text(“Get Current Location”,
style: TextStyle(fontSize: 22, color: Colors.white)),
onPressed: () {
getCurrentLocation();
},
style: ElevatedButton.styleFrom(primary: Colors.red),
),
],
),


