Skip to content
This repository was archived by the owner on Sep 19, 2021. It is now read-only.

Commit 3ea3468

Browse files
committed
Add possiblity to change the marker icon depending on the location accurency
1 parent 89be406 commit 3ea3468

9 files changed

+45
-30
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## Dev
2+
3+
* Add possiblity to change marker icon depending on the location accuracy.
4+
* BREAKING CHANGE: All options return the type `LatLngData` instead of `LatLng`.
5+
16
## 0.6.0
27

38
* Update flutter_compass to allow usage of geomagnetic rotation sensor as fallback on Android.

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ A [flutter_map](https://pub.dev/packages/flutter_map) plugin to request and disp
1313
Status
1414

1515
* [x] The location button can be changed dependening on the location services status. For example also Google Maps shows a different icon if the location service is off.
16-
* [ ] The marker icon can be changed depending on the location accuracy.
16+
* [x] The marker icon can be changed depending on the location accuracy.
1717
* [x] It's possible to show the information (e.g. in form of a snackbar) to the user that the user location is outside of the map bounds.
1818
* [x] The location heading is also shown for devices without an gyroscope. We [patched flutter_compass](https://github.com/hemanthrajv/flutter_compass/pull/38) for that.
1919

example/lib/pages/custom.dart

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import 'package:flutter/material.dart';
22
import 'package:flutter_map/flutter_map.dart';
3-
import 'package:latlong/latlong.dart';
43
import 'package:flutter_map_location/flutter_map_location.dart';
54

65
import '../widgets/drawer.dart';
@@ -40,14 +39,14 @@ class _CustomPageState extends State<CustomPage> {
4039
MarkerLayerOptions(markers: userLocationMarkers),
4140
LocationOptions(
4241
markers: userLocationMarkers,
43-
onLocationUpdate: (LatLng loc) {
44-
print('Location updated: $loc');
42+
onLocationUpdate: (LatLngData ld) {
43+
print('Location updated: ${ld?.location}');
4544
},
46-
onLocationRequested: (LatLng loc) {
47-
if (loc == null) {
45+
onLocationRequested: (LatLngData ld) {
46+
if (ld == null || ld.location == null) {
4847
return;
4948
}
50-
mapController?.move(loc, 16.0);
49+
mapController?.move(ld.location, 16.0);
5150
},
5251
buttonBuilder: (BuildContext context,
5352
ValueNotifier<LocationServiceStatus> status,
@@ -66,10 +65,10 @@ class _CustomPageState extends State<CustomPage> {
6665
),
6766
);
6867
},
69-
markerBuilder: (BuildContext context, LatLng point,
68+
markerBuilder: (BuildContext context, LatLngData ld,
7069
ValueNotifier<double> heading) {
7170
return Marker(
72-
point: point,
71+
point: ld.location,
7372
builder: (_) => Container(
7473
child: Column(
7574
children: <Widget>[

example/lib/pages/default.dart

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import 'package:flutter/material.dart';
22
import 'package:flutter_map/flutter_map.dart';
3-
import 'package:latlong/latlong.dart';
43
import 'package:flutter_map_location/flutter_map_location.dart';
54

65
import '../widgets/drawer.dart';
@@ -44,14 +43,14 @@ class _DefaultPageState extends State<DefaultPage> {
4443
// USAGE NOTE 4: Add the options for the plugin
4544
LocationOptions(
4645
markers: userLocationMarkers,
47-
onLocationUpdate: (LatLng loc) {
48-
print('Location updated: $loc');
46+
onLocationUpdate: (LatLngData ld) {
47+
print('Location updated: ${ld?.location}');
4948
},
50-
onLocationRequested: (LatLng loc) {
51-
if (loc == null) {
49+
onLocationRequested: (LatLngData ld) {
50+
if (ld == null || ld.location == null) {
5251
return;
5352
}
54-
mapController?.move(loc, 16.0);
53+
mapController?.move(ld.location, 16.0);
5554
},
5655
buttonBuilder: (BuildContext context,
5756
ValueNotifier<LocationServiceStatus> status,

lib/flutter_map_location.dart

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ export 'src/location_layer.dart';
44
export 'src/location_marker.dart';
55
export 'src/location_options.dart';
66
export 'src/location_plugin.dart';
7+
export 'src/types.dart';

lib/src/location_layer.dart

+9-7
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@ import 'package:flutter/material.dart';
44
import 'package:flutter/widgets.dart';
55
import 'package:flutter_compass/flutter_compass.dart';
66
import 'package:flutter_map/plugin_api.dart';
7+
import 'package:flutter_map_location/src/types.dart';
78
import 'package:location/location.dart';
89
import 'package:latlong/latlong.dart';
910

1011
import 'location_marker.dart';
1112
import 'location_options.dart';
1213

1314
LocationMarkerBuilder _defaultMarkerBuilder =
14-
(BuildContext context, LatLng point, ValueNotifier<double> heading) {
15+
(BuildContext context, LatLngData ld, ValueNotifier<double> heading) {
1516
return Marker(
16-
point: point,
17+
point: ld.location,
1718
builder: (_) => LocationMarker(heading: heading),
1819
height: 60.0,
1920
width: 60.0,
@@ -38,7 +39,8 @@ class _LocationLayerState extends State<LocationLayer>
3839
final Location _location = Location();
3940
final ValueNotifier<LocationServiceStatus> _serviceStatus =
4041
ValueNotifier<LocationServiceStatus>(null);
41-
final ValueNotifier<LatLng> _lastLocation = ValueNotifier<LatLng>(null);
42+
final ValueNotifier<LatLngData> _lastLocation =
43+
ValueNotifier<LatLngData>(null);
4244
final ValueNotifier<double> _heading = ValueNotifier<double>(null);
4345

4446
StreamSubscription<LocationData> _onLocationChangedSub;
@@ -54,12 +56,12 @@ class _LocationLayerState extends State<LocationLayer>
5456
_initOnLocationUpdateSubscription()
5557
.then((LocationServiceStatus status) => _serviceStatus.value = status);
5658
_lastLocation.addListener(() {
57-
final LatLng loc = _lastLocation.value;
59+
final LatLngData loc = _lastLocation.value;
5860
widget.options.onLocationUpdate?.call(loc);
5961
if (widget.options.markers.isNotEmpty) {
6062
widget.options.markers.removeLast();
6163
}
62-
if (loc == null) {
64+
if (loc == null || loc.location == null) {
6365
return;
6466
}
6567
widget.options.markers.add(widget.options.markerBuilder != null
@@ -157,9 +159,9 @@ class _LocationLayerState extends State<LocationLayer>
157159
}
158160
}
159161

160-
LatLng _locationDataToLatLng(LocationData ld) {
162+
LatLngData _locationDataToLatLng(LocationData ld) {
161163
if (ld.latitude == null || ld.longitude == null) {
162164
return null;
163165
}
164-
return LatLng(ld.latitude, ld.longitude);
166+
return LatLngData(LatLng(ld.latitude, ld.longitude), ld.accuracy);
165167
}

lib/src/location_options.dart

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import 'package:flutter/widgets.dart';
22
import 'package:flutter_map/flutter_map.dart';
33
import 'package:flutter_map/plugin_api.dart';
4-
5-
import 'package:latlong/latlong.dart';
4+
import 'package:flutter_map_location/src/types.dart';
65

76
enum LocationServiceStatus {
87
disabled,
@@ -16,7 +15,7 @@ typedef LocationButtonBuilder = Widget Function(BuildContext context,
1615
ValueNotifier<LocationServiceStatus>, Function onPressed);
1716

1817
typedef LocationMarkerBuilder = Marker Function(
19-
BuildContext context, LatLng point, ValueNotifier<double> heading);
18+
BuildContext context, LatLngData ld, ValueNotifier<double> heading);
2019

2120
class LocationOptions extends LayerOptions {
2221
LocationOptions(
@@ -29,8 +28,8 @@ class LocationOptions extends LayerOptions {
2928
: assert(markers != null, buttonBuilder != null),
3029
super();
3130

32-
final void Function(LatLng) onLocationUpdate;
33-
final void Function(LatLng) onLocationRequested;
31+
final void Function(LatLngData) onLocationUpdate;
32+
final void Function(LatLngData) onLocationRequested;
3433
final LocationButtonBuilder buttonBuilder;
3534
final LocationMarkerBuilder markerBuilder;
3635
final int updateIntervalMs;

lib/src/types.dart

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import 'package:latlong/latlong.dart';
2+
3+
class LatLngData {
4+
const LatLngData(this.location, this.accuracy);
5+
6+
final LatLng location;
7+
8+
/// Estimated horizontal accuracy, radial, in meters.
9+
final double accuracy;
10+
}

test/app_test.dart

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import 'package:flutter/material.dart';
22
import 'package:flutter_map_location/flutter_map_location.dart';
3+
import 'package:flutter_map_location/src/types.dart';
34
import 'package:flutter_test/flutter_test.dart';
45
import 'package:flutter_map/flutter_map.dart';
5-
import 'package:latlong/latlong.dart';
66

77
void main() {
88
testWidgets('Render app', (WidgetTester tester) async {
@@ -35,8 +35,8 @@ class _TestApp extends StatelessWidget {
3535
MarkerLayerOptions(markers: userLocationMarkers),
3636
LocationOptions(
3737
markers: userLocationMarkers,
38-
onLocationUpdate: (LatLng loc) {},
39-
onLocationRequested: (LatLng loc) {},
38+
onLocationUpdate: (LatLngData ld) {},
39+
onLocationRequested: (LatLngData ld) {},
4040
buttonBuilder: (BuildContext context,
4141
ValueNotifier<LocationServiceStatus> status,
4242
Function onPressed) {

0 commit comments

Comments
 (0)