location 2.3.2 location: ^2.3.2 copied to clipboard
A Flutter plugin to easily handle realtime location in iOS and Android. Provides settings for optimizing performance or battery.
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:location/location.dart';
//import 'config.dart';
void main() {
runApp(new MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
LocationData _startLocation;
LocationData _currentLocation;
StreamSubscription<LocationData> _locationSubscription;
Location _locationService = new Location();
bool _permission = false;
String error;
bool currentWidget = true;
Image image1;
@override
void initState() {
super.initState();
initPlatformState();
}
// Platform messages are asynchronous, so we initialize in an async method.
initPlatformState() async {
await _locationService.changeSettings(accuracy: LocationAccuracy.HIGH, interval: 1000);
LocationData location;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
bool serviceStatus = await _locationService.serviceEnabled();
print("Service status: $serviceStatus");
if (serviceStatus) {
_permission = await _locationService.requestPermission();
print("Permission: $_permission");
if (_permission) {
location = await _locationService.getLocation();
print("Location: ${location.latitude}");
_locationSubscription = _locationService.onLocationChanged().listen((LocationData result) {
if(mounted){
setState(() {
_currentLocation = result;
});
}
});
}
} else {
bool serviceStatusResult = await _locationService.requestService();
print("Service status activated after request: $serviceStatusResult");
if(serviceStatusResult){
initPlatformState();
}
}
} on PlatformException catch (e) {
print(e);
if (e.code == 'PERMISSION_DENIED') {
error = e.message;
} else if (e.code == 'SERVICE_STATUS_ERROR') {
error = e.message;
}
location = null;
}
setState(() {
_startLocation = location;
});
}
slowRefresh() async {
_locationSubscription.cancel();
await _locationService.changeSettings(accuracy: LocationAccuracy.BALANCED, interval: 10000);
_locationSubscription = _locationService.onLocationChanged().listen((LocationData result) {
if(mounted){
setState(() {
_currentLocation = result;
});
}
});
}
@override
Widget build(BuildContext context) {
List<Widget> widgets;
if (_currentLocation == null) {
widgets = new List();
} else {
widgets = [
// new Image.network(
// "https://maps.googleapis.com/maps/api/staticmap?center=${_currentLocation.latitude},${_currentLocation.longitude}&zoom=18&size=640x400&key=$API_KEY")
];
}
widgets.add(new Center(
child: new Text(_startLocation != null
? 'Start location: ${_startLocation.latitude} & ${_startLocation.longitude}\n'
: 'Error: $error\n')));
widgets.add(new Center(
child: new Text(_currentLocation != null
? 'Continuous location: ${_currentLocation.latitude} & ${_currentLocation.longitude} & ${_currentLocation.heading}\n'
: 'Error: $error\n')));
widgets.add(new Center(
child: new Text(_permission
? 'Has permission : Yes'
: "Has permission : No")));
widgets.add(new Center(
child: new RaisedButton(
child: new Text("Slow refresh rate and accuracy"),
onPressed: () => slowRefresh()
)
));
return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: new Text('Location plugin example app'),
),
body: new Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: widgets,
),
floatingActionButton: new FloatingActionButton(
onPressed: () => _locationSubscription.cancel(),
tooltip: 'Stop Track Location',
child: Icon(Icons.stop),
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
)
);
}
}