DEV Community

FlutterCorner
FlutterCorner

Posted on • Edited on

How to Get Device Lat Long in Flutter - fluttercorner.com

Hello Guys, How Are You All? Hope You All Are Very Well. Welcome to fluttercorner.com 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
Enter fullscreen mode Exit fullscreen mode

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" />
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

Now, import geolocation.dart in your file where you want lat and long.

import 'package:geolocation/geolocation.dart';
Enter fullscreen mode Exit fullscreen mode

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();
        });
      }
    });
  }
Enter fullscreen mode Exit fullscreen mode

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.

Full Source Code At Here https://fluttercorner.com/how-to-get-device-lat-long-in-flutter/

Top comments (2)

Collapse
 
orthymarjan profile image
Marjan Ferdousi

Is this free to use? Most of the location based services I've gone through are expensive when used in mass scale.

Collapse
 
fluttercorner profile image
FlutterCorner

Yes Definitely It is free.