Reputation: 21
Geolocator position does not appear in the console and there is no errors also I have tried to get the longitude and latitude using position.longitude separately but nothing changes , the problem is with this line but I am not able to determine it : Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
and here is the code:
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.high);
print(position);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: RaisedButton(
onPressed: () {
getLocation();
// _determinePosition();
//Get the current location
},
child: Text('Get Location'),
),
),
);
}
}
Upvotes: 1
Views: 290
Reputation: 683
Add following permission in AndroidManifest.xml for accessing location.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
Check for service enable or not.
bool servicestatus = await Geolocator.isLocationServiceEnabled();
if(servicestatus){
print("GPS service is enabled");
}else{
print("GPS service is disabled.");
}
Now try to fetch location details.
Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
print(position.longitude); //Output: 80.24599079
print(position.latitude); //Output: 29.6593457
Upvotes: 1