Skip to content

Commit

Permalink
Issue 290 : Add example of showing current location
Browse files Browse the repository at this point in the history
  • Loading branch information
raacker committed Oct 16, 2019
1 parent e8e687b commit eb0cf9c
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 3 deletions.
5 changes: 2 additions & 3 deletions example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ apply plugin: 'com.android.application'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
compileSdkVersion 25
buildToolsVersion '25.0.3'
compileSdkVersion 28

lintOptions {
disable 'InvalidPackage'
Expand All @@ -26,7 +25,7 @@ android {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.johnpryan.leafletflutterexample"
minSdkVersion 16
targetSdkVersion 25
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
Expand Down
1 change: 1 addition & 0 deletions example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
to allow setting breakpoints, to provide hot reload, etc.
-->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<!-- io.flutter.app.FlutterApplication is an android.app.Application that
calls FlutterMain.startInitialization(this); in its onCreate method.
Expand Down
1 change: 1 addition & 0 deletions example/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ buildscript {

dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
classpath 'com.google.gms:google-services:4.2.0'
}
}

Expand Down
3 changes: 3 additions & 0 deletions example/android/gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
org.gradle.jvmargs=-Xmx1536M
android.enableR8=true
android.enableJetifier=true
android.useAndroidX=true
2 changes: 2 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import './pages/animated_map_controller.dart';
import './pages/circle.dart';
import './pages/esri.dart';
import './pages/home.dart';
import './pages/live_location.dart';
import './pages/map_controller.dart';
import './pages/marker_anchor.dart';
import './pages/moving_markers.dart';
Expand Down Expand Up @@ -44,6 +45,7 @@ class MyApp extends StatelessWidget {
MovingMarkersPage.route: (context) => MovingMarkersPage(),
CirclePage.route: (context) => CirclePage(),
OverlayImagePage.route: (context) => OverlayImagePage(),
LiveLocationPage.route: (context) => LiveLocationPage(),
},
);
}
Expand Down
159 changes: 159 additions & 0 deletions example/lib/pages/live_location.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:latlong/latlong.dart';
import 'package:location/location.dart';

class LiveLocationPage extends StatefulWidget {
static const String route = '/live_location';

@override
_LiveLocationPageState createState() => _LiveLocationPageState();
}

class _LiveLocationPageState extends State<LiveLocationPage> {

LocationData _currentLocation;
MapController _mapController;

bool _liveUpdate = true;
bool _permission = false;

String _serviceError = '';

final Location _locationService = Location();

@override
void initState() {
super.initState();
_mapController = MapController();
initLocationService();
}

void initLocationService() async {
await _locationService.changeSettings(
accuracy: LocationAccuracy.HIGH,
interval: 1000,
);

LocationData location;
bool serviceEnabled;
bool serviceRequestResult;

try {
serviceEnabled = await _locationService.serviceEnabled();

if (serviceEnabled) {
_permission = await _locationService.requestPermission();

if (_permission) {
location = await _locationService.getLocation();
_currentLocation = location;
_locationService.onLocationChanged().listen((LocationData result) async {
if (mounted) {
setState(() {
_currentLocation = result;

// If Live Update is enabled, move map center
if (_liveUpdate) {
_mapController.move(
LatLng(
_currentLocation.latitude,
_currentLocation.longitude),
_mapController.zoom
);
}
});
}
});
}
} else {
serviceRequestResult = await _locationService.requestService();
if(serviceRequestResult){
initLocationService();
return;
}
}
} on PlatformException catch (e) {
print(e);
if (e.code == 'PERMISSION_DENIED') {
_serviceError = e.message;
} else if (e.code == 'SERVICE_STATUS_ERROR') {
_serviceError = e.message;
}
location = null;
}
}

@override
Widget build(BuildContext context) {
LatLng currentLatLng;

// Until currentLocation is initially updated, Widget can locate to 0, 0
// by default or store previous location value to show.
if (_currentLocation != null) {
currentLatLng = LatLng(_currentLocation.latitude, _currentLocation.longitude);
} else {
currentLatLng = LatLng(0, 0);
}

var markers = <Marker>[
Marker(
width: 80.0,
height: 80.0,
point: currentLatLng,
builder: (ctx) => Container(
child: FlutterLogo(
colors: Colors.blue,
key: ObjectKey(Colors.blue),
),
),
),
];

return Scaffold(
appBar: AppBar(title: Text('Home')),
//drawer: buildDrawer(context, route),
body: Padding(
padding: EdgeInsets.all(8.0),
child: Column(
children: [
Padding(
padding: EdgeInsets.only(top: 8.0, bottom: 8.0),
child: _serviceError.isEmpty ?
Text('This is a map that is showing '
'(${currentLatLng.latitude}, ${currentLatLng.longitude}).') :
Text('Error occured while acquiring location. Error Message : '
'$_serviceError'),
),
Flexible(
child: FlutterMap(
mapController: _mapController,
options: MapOptions(
center: LatLng(currentLatLng.latitude, currentLatLng.longitude),
zoom: 5.0,
),
layers: [
TileLayerOptions(
urlTemplate:
'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
subdomains: ['a', 'b', 'c'],
// For example purposes. It is recommended to use
// TileProvider with a caching and retry strategy, like
// NetworkTileProvider or CachedNetworkTileProvider
tileProvider: NonCachingNetworkTileProvider(),
),
MarkerLayerOptions(markers: markers)
],
),
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () => _liveUpdate = !_liveUpdate,
child: _liveUpdate ? Icon(Icons.location_on) : Icon(Icons.location_off),
),
);
}
}
8 changes: 8 additions & 0 deletions example/lib/widgets/drawer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import '../pages/animated_map_controller.dart';
import '../pages/circle.dart';
import '../pages/esri.dart';
import '../pages/home.dart';
import '../pages/live_location.dart';
import '../pages/map_controller.dart';
import '../pages/marker_anchor.dart';
import '../pages/moving_markers.dart';
Expand Down Expand Up @@ -132,6 +133,13 @@ Drawer buildDrawer(BuildContext context, String currentRoute) {
Navigator.pushReplacementNamed(context, OverlayImagePage.route);
},
),
ListTile(
title: const Text('Live Location Update'),
selected: currentRoute == LiveLocationPage.route,
onTap: () {
Navigator.pushReplacementNamed(context, LiveLocationPage.route);
},
),
],
),
);
Expand Down
1 change: 1 addition & 0 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ dependencies:
cupertino_icons: ^0.1.0
flutter_map:
path: ../
location: ^2.3.5

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit eb0cf9c

Please sign in to comment.