In this course, we will learn how to install the Geolocator package in a Flutter application. The Geolocator package is a useful Flutter package that tracks the user’s location and provides various GPS-based features. With this package, the application can easily utilize the user’s location information and implement various features such as maps and location-based services.
1. What is the Geolocator package?
Geolocator is a package that helps easily navigate and utilize location in Flutter applications. By using this package, you can obtain the user’s location in real time via GPS, network, or other sensors. Geolocator works on both Android and iOS, supporting features such as:
- Getting the current location
- Receiving location updates
- Managing location permissions
- Calculating distances and verifying location distances
2. How to install the Geolocator package
To install the Geolocator package, you need to follow these steps.
2.1 Creating a Flutter project
If you haven’t created a Flutter project yet, create a new Flutter project with the following command:
flutter create my_location_app
Navigate to the project directory:
cd my_location_app
2.2 Adding the Geolocator package
Open the project’s pubspec.yaml
file and add geolocator
to the dependencies
list. You should check for the latest version and add the appropriate version. Here is an example:
dependencies:
flutter:
sdk: flutter
geolocator: ^9.0.0
After adding it, run the following command to install the dependencies:
flutter pub get
3. Setting up the Geolocator package
After installing the Geolocator package, you need to add some settings to use it in your application.
3.1 Android settings
To request location permissions on Android, open the AndroidManifest.xml
file and add the following permissions:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
3.2 iOS settings
For iOS, modify the Info.plist
file to add a message for requesting location permissions. Please add the following:
<key>NSLocationWhenInUseUsageDescription</key>
<string>Permission is required to use the user's location.</string>
4. Implementing basic location code
Now let’s write the basic code to get the user’s location using the Geolocator package.
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: LocationScreen(),
);
}
}
class LocationScreen extends StatefulWidget {
@override
_LocationScreenState createState() => _LocationScreenState();
}
class _LocationScreenState extends State {
String locationMessage = "";
@override
void initState() {
super.initState();
getLocation();
}
Future getLocation() async {
Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
setState(() {
locationMessage = "Latitude: ${position.latitude}, Longitude: ${position.longitude}";
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Location Information'),
),
body: Center(
child: Text(locationMessage),
),
);
}
}
Using the code above, you can create a Flutter application that retrieves the user’s current location (latitude and longitude).
5. Conclusion
We learned how to install and set up the Geolocator package. This allows us to efficiently utilize the user’s location information in a Flutter application. Additionally, you can use various methods of the Geolocator package to implement location tracking and other features.