12

How can i get current location on android device using flutter. I have tried both Location and GeoLocator plugins the GeoLocator 3.0.0 shows this error on debuging:

Launching lib\main.dart on Android SDK built for x86 in debug mode...

FAILURE: Build failed with an exception.

  • What went wrong: Execution failed for task ':app:preDebugBuild'.

    Android dependency 'android.arch.lifecycle:runtime' has different version for the compile (1.0.0) and runtime (1.1.0) classpath. You should manually set the same version via DependencyResolution

Location 2.0.0 also throws an error on debugging. There is another plugin available named geoLocation but that is not compatible with dart 2.0. In such a situation how can i get location (longitude and latitude)[once]? Any help would be highly appreciated.

2
  • Did you added <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> permission to AndroidManifest.xml ? Feb 28, 2019 at 13:16
  • Yup. There isn't any problem with permission. I also have used uses_permission plugin for Android 6+ devices to explicitly request permission. Feb 28, 2019 at 14:04

4 Answers 4

20

Follow these steps to get location in both IOS and Android.

  1. Add geolocator: ^4.0.3 in your pubspec.yaml file below dependencies: tag
  2. Add import 'package:geolocator/geolocator.dart'; in your dart file where you need to get location
  3. In order to get location for android add following permissions in android project manifest file

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

  1. For IOS you'll need to add following lines in your info.plist file under <dict> in IOS project

    <key>NSLocationWhenInUseUsageDescription</key>

    <string>This app needs access to location when open.</string>

  2. Function to get location

    void getLocation() async { Position position = await Geolocator .getCurrentPosition(desiredAccuracy: LocationAccuracy.high); print(position); }

Cheers!

2
  • For me it is still failing. Getting this error. E/location_permissions(24439): Flutter result object is null.
    – Roxx
    Apr 28, 2020 at 10:17
  • 1
    still not getting current location
    – s.j
    Aug 27, 2020 at 5:32
14
  1. Add GeoLocation Plugin in pubsec.yaml
    geolocator: ^6.0.0

  2. Run flutter pub get

  3. Add location permissions for Android in app/src/main/AndroidManifest.xml

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
  1. For iOS add following keys in info.plist
<key>NSLocationWhenInUseUsageDescription</key>
<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>
  1. Creart function to fetch user current location:
LatLng currentPostion;

void _getUserLocation() async {
        var position = await GeolocatorPlatform.instance
            .getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
    
        setState(() {
          currentPostion = LatLng(position.latitude, position.longitude);
        });
      }
  1. Use currentPostion in your googlemap. (define Latlag currentPosition);
GoogleMap(
             // onMapCreated: _onMapCreated,
             initialCameraPosition: CameraPosition(
               target: currentPostion,
               zoom: 10,
             ),  
           ),
1
  • Number 3 worked for me! i tried only with one before, not the 3 of them at the same time :)... Thanks! Jan 14, 2021 at 5:40
3

It may be possible that uptil now you might have found a solution but still I am adding this so that others can get help

use geolocator for getting current location

In pubsec.yaml

  geolocator: '^2.0.1'

In your dart file

import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';

void main() => runApp(MapScreen());

class MapScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "Map",
      home: MapActivity(),
    );
  }
}

class MapActivity extends StatefulWidget {
  @override
  _MapActivityState createState() => _MapActivityState();
}

class _MapActivityState extends State<MapActivity> {
  LatLng _center ;
  Position currentLocation;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getUserLocation();
  }

 Future<Position> locateUser() async {
    return Geolocator()
        .getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
  }

  getUserLocation() async {
    currentLocation = await locateUser();
    setState(() {
      _center = LatLng(currentLocation.latitude, currentLocation.longitude);
    });
    print('center $_center');
  }
}

Hope this helps

1
  • Thanks. I had this issue because of libraries conflict and updating my project to Android x libraries resolved the error. Apr 11, 2019 at 15:13
1

The geoLocator plugin that I installed was using Android x jetpack libraries while my project was using support libraries so this conflict was causing the problem and when I updated my project to use Android x libraries the problem was resolved.

Not the answer you're looking for? Browse other questions tagged or ask your own question.