Hello Guys, How Are You All ? Hope You All Are Very Well. As a Flutter developer Sometimes you need device current lattitude and Longitude. So in This tutorial we are going to learn how to get device lat long in flutter

How to Get Device Lat Long in Flutter
- First of all. All You Need is GeoLocation package. So define on pubspec.yaml file and then get that dependencies. Same like below.
dependencies:
flutter:
sdk: flutter
geolocation: ^1.1.2
- Now. For Android Users, you Must have to define below both line in your Flutter_project_path/android/app/src/main/AndroidManifest.xml File Exactly Like below.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
- For iOS users, You need to declare the description for the desired permission in
ios/Runner/Info.plist
:
<dict>
<!-- for iOS 11 + -->
<key>NSLocationWhenInUseUsageDescription</key>
<string>Reason why app needs location</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Reason why app needs location</string>
<!-- additionally for iOS 9/10, if you need always permission -->
<key>NSLocationAlwaysUsageDescription</key>
<string>Reason why app needs location</string>
...
</dict>
- Now, import geolocation.dart in your file where you want lat and long.
import 'package:geolocation/geolocation.dart';
- Define latitude and longitude that you want to use. In my case i need lat long in String Formate so i am defining String.
String latitude = '00.00000';
String longitude = '00.00000';
- Now Create Method named _getCurrentLocation with async Like Below.
_getCurrentLocation() async {
Geolocation.enableLocationServices().then((result) {
// Request location
// print(result);
}).catchError((e) {
// Location Services Enablind Cancelled
// print(e);
});
Geolocation.currentLocation(accuracy: LocationAccuracy.best)
.listen((result) {
if (result.isSuccessful) {
setState(() {
latitude = result.location.latitude.toString();
longitude = result.location.longitude.toString();
});
}
});
}
- Now Trigger This Method where you wanna use. If users allow for location it will give you lat long
- also you can customize accuracy: LocationAccuracy.best by refering its Documentation file.
- i am giving my source code here that will help you to understand.
import 'package:flutter/material.dart';
import 'package:geolocation/geolocation.dart';
class GetUserLatLong extends StatefulWidget {
@override
_GetUserLatLongState createState() => _GetUserLatLongState();
}
class _GetUserLatLongState extends State<GetUserLatLong> {
String latitude = '00.00000';
String longitude = '00.00000';
_getCurrentLocation() async {
Geolocation.enableLocationServices().then((result) {
// Request location
// print(result);
}).catchError((e) {
// Location Services Enablind Cancelled
// print(e);
});
Geolocation.currentLocation(accuracy: LocationAccuracy.best)
.listen((result) {
if (result.isSuccessful) {
setState(() {
latitude = result.location.latitude.toString();
longitude = result.location.longitude.toString();
});
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Location"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("Lattitude is : ${latitude} And Longitude Is: ${longitude}"),
FlatButton(
child: Text("Get location"),
onPressed: () {
_getCurrentLocation();
},
),
],
),
),
);
}
}
- How to get Lat Long in flutter ?
As Flutter Developer sometimes we need device current latitude and longitude. First of all you need is GeoLocation package So that add geoLocation plugin in pubsec.yaml file and then get dependencies. As like below
- get Lat Long in flutter
As Flutter Developer sometimes we need device current latitude and longitude. First of all you need is GeoLocation package So that add geoLocation plugin in pubsec.yaml file and then get dependencies. As like below
Summery
So, It’s All About How to Get Device Lat Long in Flutter. I hope this tutorial helps you lot. Please Comment Below if You stucks anywhere with my code. Thank You.
Leave a Reply