Flutter-geolocator: Geolocator.getCurrentPosition() is not returning latitude or longitude.

Created on 28 Nov 2020  ·  3Comments  ·  Source: Baseflow/flutter-geolocator

Geolocator.getCurrentPosition() is not working for emulator. However, it is working on a real device.

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

class LoadingScreen extends StatefulWidget {
  @override
  _LoadingScreenState createState() => _LoadingScreenState();
}

class _LoadingScreenState extends State<LoadingScreen> {
  void getLocation() async {
    Position position = await Geolocator.getCurrentPosition(
      desiredAccuracy: LocationAccuracy.low,
    );

    print(position);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: RaisedButton(
          onPressed: () {
            getLocation();
          },
          child: Text('Get Location'),
        ),
      ),
    );
  }
}

This is the code and print(position) is not printing latitudes and longitudes.

needs more info

Most helpful comment

I am working on an Android emulator. I don't need to get a high accuracy location so I only requested permission for accessing the coarse location. After I saw @mvanbeusekom's comment I wanted to try LocationAccuracy,high after adding the permission needed which is <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> to the manifest.

However after adding that permission, BOTH low and high accuracy requests worked for me. Interesting.

All 3 comments

Are you testing on iOS or Android?

B.t.w. make sure you enable location updates on the simulator/ emulator. This can be done in the settings of the simulator/ emulator.

Also the simulator/ emulator doesn't always play nice with LocationAccuracy.low, you could try to move this to a higher level.

I am working on an Android emulator. I don't need to get a high accuracy location so I only requested permission for accessing the coarse location. After I saw @mvanbeusekom's comment I wanted to try LocationAccuracy,high after adding the permission needed which is <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> to the manifest.

However after adding that permission, BOTH low and high accuracy requests worked for me. Interesting.

I will close this issue for now. It is a known issue that the Android emulator doesn't play nice with the COARSE permissions. This is due to the fact that COARSE location tries to triangulate the position based on cellular network, which on the emulator is not available.

Was this page helpful?
0 / 5 - 0 ratings