Here’s an example of how to use the geolocator package in Flutter:
First, add the following dependency to your pubspec.yaml file:
dependencies:
flutter:
sdk: flutter
geolocator: ^14.0.2
Then, run flutter pub get to install the package.
Now, here’s an example of how to use it in a Flutter app:
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String _currentPosition = '';
bool _isLocationServiceEnabled = false;
bool _isGeolocationServiceEnabled = false;
@override
void initState() {
super.initState();
_checkLocationService();
}
Future<void> _checkLocationService() async {
bool serviceEnabled;
LocationPermission permission;
serviceEnabled = await Geolocator.isLocationServiceEnabled();
if (!serviceEnabled) {
// Open the settings menu
_openAppSettings();
}
permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
if (permission == LocationPermission.denied) {
setState(() {
_isLocationServiceEnabled = false;
});
} else {
setState(() {
_isLocationServiceEnabled = true;
});
}
} else if (permission == LocationPermission.deniedForever) {
setState(() {
_isLocationServiceEnabled = false;
});
} else {
setState(() {
_isLocationServiceEnabled = true;
});
}
}
Future<void> _openAppSettings() async {
await launchUrl(Uri.parse('https://support.google.com/locationhistory/answer/1719578'));
}
Future<void> _getCurrentPosition() async {
Position position = await Geolocator.getCurrentPosition(desiredAccuracy: DesiredAccuracy.high);
setState(() {
_currentPosition = 'Latitude: ${position.latitude}, Longitude: ${position.longitude}';
});
}
Future<void> _getLastKnownPosition() async {
Position position = await Geolocator.getLastKnownPosition(desiredAccuracy: DesiredAccuracy.high);
setState(() {
_currentPosition = 'Latitude: ${position.latitude}, Longitude: ${position.longitude}';
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Geolocator Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(_currentPosition),
SizedBox(height: 20),
ElevatedButton(
onPressed: _isLocationServiceEnabled? _getCurrentPosition : null,
child: Text('Get Current Position'),
),
SizedBox(height: 10),
ElevatedButton(
onPressed: _isLocationServiceEnabled? _getLastKnownPosition : null,
child: Text('Get Last Known Position'),
),
],
),
),
);
}
}
This example shows how to:
- Check if the location service is enabled.
- Request permission for the app to access the user’s location.
- Get the current position of the device.
- Get the last known position of the device.
Note that you need to add the geolocator package and import it in your Dart file for this example to work.
Also, remember that the geolocator package requires the user’s permission to access their location. If you’re testing this on a physical device, make sure to grant the app permission in your device’s settings. If you’re testing this on an emulator, make sure to add the necessary permissions in your AndroidManifest.xml file.
