SkyFall3N
SkyFall3N

Reputation: 31

Flutter Geolocator Location doesn't obtain for real phones

I used geolocator flutter package for my little app and it works perfectly with emulator, but when I try to use in my real smart phone, it doesnt work. Do you have any idea?

void getLocationData() async {
    WeatherModel weatherModel = WeatherModel();
    var weatherData = await weatherModel.getLocationWeather();
    //print(weatherData);
    var zipCode = await weatherModel.getLocationZipCode();
    zipCode = zipCode.toString().substring(0, 2);

    Navigator.push(context, MaterialPageRoute(builder: (context) {
      return LocationScreen(
        locationWeather: weatherData,
        zipCode: zipCode,
      );
    }));
  }
Future<dynamic> getLocationWeather() async {
    Location location = Location();
    await location.getCurrentLocation();

    NetworkHelper networkHelper = NetworkHelper(
        '$openWeatherMapURL?lat=${location.latitude}&lon=${location.longitude}&appid=$apiKey&units=metric');

    var weatherData = await networkHelper.getData();
    return weatherData;
  }

Upvotes: 0

Views: 4511

Answers (4)

uğur yilmaz
uğur yilmaz

Reputation: 1

using geolocator: ^7.0.1
and adding

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

to AndroidManifest.xml file

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

and using in onPressed

onPressed: () {
        getLocation();
      },

solves the problem.

Upvotes: 0

Angela Yu's student? her projects need one update for optimizing and fixing little errors.

I got problems with Geolocator, it was not being used correctly, and this site helped me a lot. Then I made my own Location class with Provider and... life becomes good again.

Upvotes: 2

You should add

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

to the AndroidManifest.xml. It helps to use the internet to fetch location data in an Android device, because your app still need to get data from URL openweathermap.

Upvotes: 0

Omer Gamliel
Omer Gamliel

Reputation: 506

Try use geolocator. I seems you are not using geolocator package

final Geolocator geolocator = Geolocator()..forceAndroidLocationManager;
// return true if location service is enable, false if not
bool locationServiceEnable = await geolocator.isLocationServiceEnabled();
// position instace
Position position;

// if location service enable get current position
if (locationServiceEnable) {
  // await current position
  position = await geolocator.getCurrentPosition(
      desiredAccuracy: LocationAccuracy.best);
  // if location service is not enable get last known position
} else {
  // await last known position
  position = await geolocator.getLastKnownPosition(
      desiredAccuracy: LocationAccuracy.best);
}

if (position != null) {
  // fetch weather data with position data
}

Upvotes: 0

Related Questions