Hello friends, today in this article I am going to teach you how to get user’s current location in flutter. Its video tutorial is also on my YouTube channel, you can go by clicking here. For this I am going to use a flutter plugin.
So you have to install these two plugins in pubspec.yaml file.
geolocator: ^6.2.0
geocoding: ^1.0.5
Now you have to go android->app->src->main->AndroidManifest.xml
Open the AndroidManifest.xml
file add the following line:
<manifest xmlns:android="...">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
</manifast>
First of all we have to know the latitude and longitude for the location.
Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.best, forceAndroidLocationManager: true)
.then((Position position) {
setState(() {
_currentPosition = position;
_getAddressFromLatLng();
});
print(_currentPosition);
}).catchError((e) {
print(e);
});
Once we have got the latitude and longitude, now we can easily get its address.
try {
List<Placemark> placemarks = await placemarkFromCoordinates(
_currentPosition.latitude,
_currentPosition.longitude
);
Placemark place = placemarks[0];
setState(() {
_currentAddress = " ${place.street}, ${place.locality} ,${place.subAdministrativeArea},${place.administrativeArea}, ${place.postalCode}, ${place.country}";
print(_currentAddress);
});
} catch (e) {
print(e);
}
Full Code
import 'package:flutter/material.dart';
import 'package:geocoding/geocoding.dart';
import 'package:geolocator/geolocator.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Location'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
String _currentAddress="";
late Position _currentPosition;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
_currentAddress,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed:(){
latandlon();
},
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
void latandlon(){
Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.best, forceAndroidLocationManager: true)
.then((Position position) {
setState(() {
_currentPosition = position;
_getAddressFromLatLng();
});
print(_currentPosition);
}).catchError((e) {
print(e);
});
}
_getAddressFromLatLng() async {
try {
List<Placemark> placemarks = await placemarkFromCoordinates(
_currentPosition.latitude,
_currentPosition.longitude
);
Placemark place = placemarks[0];
setState(() {
_currentAddress = " ${place.street}, ${place.locality} ,${place.subAdministrativeArea},${place.administrativeArea}, ${place.postalCode}, ${place.country}";
print(_currentAddress);
});
} catch (e) {
print(e);
}
}
}