Reputation: 21
code that has been used.
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
class LoadingScreen extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _LoadingScreenState();
}
}
class _LoadingScreenState extends State<LoadingScreen> {
@override
void initState() {
super.initState();
getLocation();
}
void getLocation() async {
try {
Position position = await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.low);
print(position);
} catch (e) {
print(e);
}
}
@override
Widget build(BuildContext context) {
return Scaffold();
}
}
Output:
Launching lib\main.dart on AOSP on IA Emulator in debug mode...
Running Gradle task 'assembleDebug'...
✓ Built build\app\outputs\flutter-apk\app-debug.apk.
Installing build\app\outputs\flutter-apk\app.apk...
Waiting for AOSP on IA Emulator to report its views...
Debug service listening on ws://127.0.0.1:51964/UEmjv7haU5w=/ws
Syncing files to device AOSP on IA Emulator...
I/Choreographer(14504): Skipped 116 frames! The application may be doing too much work on its main thread.
D/EGL_emulation(14504): eglMakeCurrent: 0xe8804b00: ver 3 1 (tinfo 0xea15ec70)
D/eglCodecCommon(14504): setVertexArrayObject: set vao to 0 (0) 1 2
I/OpenGLRenderer(14504): Davey! duration=2865ms; Flags=1, IntendedVsync=25791957327046, Vsync=25793890660302, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=25793896676340, AnimationStart=25793898368240, PerformTraversalsStart=25793934314440, DrawStart=25794433733640, SyncQueued=25794660799440, SyncStart=25794663501440, IssueDrawCommandsStart=25794663679640, SwapBuffers=25794747224740, FrameCompleted=25794825369240, DequeueBufferDuration=32086000, QueueBufferDuration=859000,
D/EGL_emulation(14504): eglMakeCurrent: 0xe88045c0: ver 3 1 (tinfo 0xea15e5e0)
D/eglCodecCommon(14504): setVertexArrayObject: set vao to 0 (0) 1 0
I/flutter (14504): The location service on the device is disabled.
Upvotes: 0
Views: 872
Reputation: 492
You can use the codes :
var geolocator = Geolocator();
var locationOptions;
getPermission() async {
return Geolocator().checkGeolocationPermissionStatus();
}
then call this function in initState() :
getPermission().then((res) {
locationOptions =
LocationOptions(accuracy: LocationAccuracy.high, distanceFilter: 10);
});
geolocator.getPositionStream(locationOptions).listen((Position position) {
setState(() {
lat = position.latitude.toDouble();
long = position.longitude.toDouble();
});
});
Upvotes: 0
Reputation: 327
Your output says "The location service on the device is disabled" so may you should check the permission in the manifest file. I suggest you to use the coarse and fine permissions together.
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
But you should make a previous verification of the location service too before do geolocation-requests.
you should chechk if the service is enabled:
bool isLocationServiceEnabled = await Geolocator.isLocationServiceEnabled();
print('location enabled: $isLocationServiceEnabled');
then check if the app has permissions:
LocationPermission permission = await Geolocator.checkPermission();
print(permission);
if you don't have permissions ask for them:
LocationPermission permission = await Geolocator.requestPermission();
print(permission);
with all that set up you can make a secure request
Position position = await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.low);
print(position);
Upvotes: 1