What is the location package?
- Flutter course
- Advanced
- Lesson 36/69
- 4 min
Get the device location with the location package.
What is the location package?
The location package is used to read the device's GPS position. You create an instance of the Location class and obtain the position through this instance. The location data comes as latitude and longitude.
final location = Location();
final data = await location.getLocation();
print(data.latitude);LocationData and latitude
The getLocation method returns a LocationData object. Its latitude and longitude fields give the position coordinates. Latitude is the distance from the equator, and longitude is the distance from the prime meridian.
final data = await location.getLocation();
print(data.latitude);
print(data.longitude);Practice Create a Location instance
Create a Location instance and print 'ok'.
Starter code
class Location {
Location();
}
void main() {
// Location örneği oluştur
// 'ok' yazdır
}Sample solution
class Location {
Location();
}
void main() {
final location = Location();
print('ok');
}Expected output
ok
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 How is an instance of the Location class created?
- new Location()
- Location() Correct answer
- Location.instance
- Location.create()
Explanation: To create an instance of the Location class you call the Location() constructor; the parentheses are required.
2 Create a Location instance
final location = ____;
final location = Location();
Explanation: To create an instance of the Location class you call the Location() constructor; the parentheses are required.
3 location parts
| Term | Meaning |
|---|---|
Location |
Location service class |
LocationData |
Location data |
latitude |
Latitude |
longitude |
Longitude |