From f970c6b4feec7bfb0a43e98edd9f77b69a5aa8a7 Mon Sep 17 00:00:00 2001 From: vjrj Date: Tue, 12 Jun 2018 21:06:40 +0200 Subject: [PATCH] Refactor. Fires marker calibration --- lib/activeFires.dart | 4 +- lib/dummyMapPlugin.dart | 27 +++++++ lib/fireMarkType.dart | 7 ++ lib/fireMarker.dart | 29 ++++++++ lib/fireMarkerIcon.dart | 27 +++++++ lib/{leafletMap.dart => genericMap.dart} | 71 ++++++++----------- lib/globals.dart | 13 ++-- lib/mainDrawer.dart | 1 - ...ustomMapPlugin.dart => zoomMapPlugin.dart} | 0 9 files changed, 128 insertions(+), 51 deletions(-) create mode 100644 lib/dummyMapPlugin.dart create mode 100644 lib/fireMarkType.dart create mode 100644 lib/fireMarker.dart create mode 100644 lib/fireMarkerIcon.dart rename lib/{leafletMap.dart => genericMap.dart} (79%) rename lib/{customMapPlugin.dart => zoomMapPlugin.dart} (100%) diff --git a/lib/activeFires.dart b/lib/activeFires.dart index 4e2931c..c3870f2 100644 --- a/lib/activeFires.dart +++ b/lib/activeFires.dart @@ -1,6 +1,6 @@ import 'package:flutter/material.dart'; import 'mainDrawer.dart'; -import 'leafletMap.dart'; +import 'genericMap.dart'; import 'dart:async'; import 'package:comunes_flutter/comunes_flutter.dart'; import 'colors.dart'; @@ -80,7 +80,7 @@ class _ActiveFiresPageState extends State { Navigator.push( context, new MaterialPageRoute( - builder: (context) => new LeafletMap( + builder: (context) => new GenericMap( title: loc.description, location: loc, ))); diff --git a/lib/dummyMapPlugin.dart b/lib/dummyMapPlugin.dart new file mode 100644 index 0000000..565d549 --- /dev/null +++ b/lib/dummyMapPlugin.dart @@ -0,0 +1,27 @@ +import 'package:flutter/src/widgets/framework.dart'; +import 'package:flutter_map/plugin_api.dart'; +import 'package:flutter/material.dart'; + +class DummyMapPluginOptions extends LayerOptions { + final String text; + + DummyMapPluginOptions({this.text = ""}); +} + +// https://github.com/apptreesoftware/flutter_map/blob/master/flutter_map_example/lib/pages/plugin_api.dart +class DummyMapPlugin extends MapPlugin { + + @override + Widget createLayer(LayerOptions options, MapState mapState) { + if (options is DummyMapPluginOptions) { + return const SizedBox(); + } + throw ("Unknown options type for DummyMapPlugin" + "plugin: $options"); + } + + @override + bool supportsLayer(LayerOptions options) { + return options is DummyMapPluginOptions; + } +} diff --git a/lib/fireMarkType.dart b/lib/fireMarkType.dart new file mode 100644 index 0000000..ba953bc --- /dev/null +++ b/lib/fireMarkType.dart @@ -0,0 +1,7 @@ +enum FireMarkType { + position, // user saved location + pixel, // use for fire drawn as pixels + fire, + industry, + falsePos +} \ No newline at end of file diff --git a/lib/fireMarker.dart b/lib/fireMarker.dart new file mode 100644 index 0000000..d193e94 --- /dev/null +++ b/lib/fireMarker.dart @@ -0,0 +1,29 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_map/flutter_map.dart'; +import 'package:flutter_map/plugin_api.dart'; +import 'package:latlong/latlong.dart'; +import 'fireMarkerIcon.dart'; +import 'fireMarkType.dart'; + +class FireMarker extends Marker { + FireMarker(location, type, + [AnchorPos anchor = AnchorPos.center, Anchor anchorOverride]) + : super( + width: 80.0, + height: 80.0, + point: new LatLng(location.lat, location.lon), + builder: (ctx) => new Container( + child: new FireMarkerIcon(type), + ), + anchor: anchor, + anchorOverride: anchorOverride ?? type == FireMarkType.position + ? new Anchor(40.0, 20.0) + : type == FireMarkType.fire + ? new Anchor(40.0, 24.0) + : type == FireMarkType.pixel + ? null // auto calculate with height/width in Marker + // industries, etc (below) + // FIXME check if this is accurate + : new Anchor(40.0, 35.0), + ); +} diff --git a/lib/fireMarkerIcon.dart b/lib/fireMarkerIcon.dart new file mode 100644 index 0000000..16b361b --- /dev/null +++ b/lib/fireMarkerIcon.dart @@ -0,0 +1,27 @@ +import 'package:flutter/material.dart'; +import 'fireMarkType.dart'; +import 'colors.dart'; + +class FireMarkerIcon extends StatelessWidget { + + final FireMarkType type; + + FireMarkerIcon(this.type); + + @override + Widget build(BuildContext context) { + switch (type) { + case FireMarkType.position: + return new Icon(Icons.location_on, color: fires600, size: 50.0); + case FireMarkType.pixel: + return new Icon(Icons.brightness_1, color: fires900, size: 3.0); + case FireMarkType.fire: + return new Image.asset('images/fire-marker-l.png'); + case FireMarkType.industry: + return new Image.asset('images/industry-marker-reg.png'); + case FireMarkType.falsePos: + default: + return new Image.asset('images/industry-marker.png'); + } + } +} diff --git a/lib/leafletMap.dart b/lib/genericMap.dart similarity index 79% rename from lib/leafletMap.dart rename to lib/genericMap.dart index 7fd42ee..98bddde 100644 --- a/lib/leafletMap.dart +++ b/lib/genericMap.dart @@ -11,23 +11,26 @@ import 'globals.dart' as globals; import 'package:http/http.dart' as http; import 'dart:convert' show json; import 'slider.dart'; -import 'customMapPlugin.dart'; +import 'fireMarker.dart'; +import 'zoomMapPlugin.dart'; +import 'dummyMapPlugin.dart'; import 'package:just_debounce_it/just_debounce_it.dart'; +import 'fireMarkType.dart'; -enum MarkType { position, fire, industry, falsePos } +enum MapOperation { view } -class LeafletMap extends StatefulWidget { +class GenericMap extends StatefulWidget { final BasicLocation location; final String title; - LeafletMap({@required this.title, @required this.location}); + GenericMap({@required this.title, @required this.location}); @override - _LeafletMapState createState() => - _LeafletMapState(title: title, location: location); + _GenericMapState createState() => + _GenericMapState(title: title, location: location); } -class _LeafletMapState extends State { +class _GenericMapState extends State { final GlobalKey _scaffoldKey = new GlobalKey(); final BasicLocation location; @@ -50,7 +53,7 @@ class _LeafletMapState extends State { http.read(url).then((result) { setState(() { var resultDecoded = json.decode(result); - print(resultDecoded); + // print(resultDecoded); numFires = resultDecoded['real']; fires = resultDecoded['fires']; falsePos = resultDecoded['falsePos']; @@ -60,19 +63,21 @@ class _LeafletMapState extends State { var industriesCount = industries.length; var falsePosCount = falsePos.length; - print( - 'fire: $firesCount falsePos: $falsePosCount industries: $industriesCount'); + /* print( + 'fire: $firesCount falsePos: $falsePosCount industries: $industriesCount'); */ }); }); } - _LeafletMapState({@required this.title, @required this.location}); + _GenericMapState({@required this.title, @required this.location}); @override Widget build(BuildContext context) { MapOptions mapOptions = new MapOptions( center: new LatLng(this.location.lat, this.location.lon), - plugins: [new ZoomMapPlugin()], + plugins: globals.isDevelopment + ? [new ZoomMapPlugin()] + : [new DummyMapPlugin()], // this works ? // interactive: false, zoom: 13.0, @@ -99,7 +104,9 @@ class _LeafletMapState extends State { '© OpenStreetMap contributors' }, ), - new ZoomMapPluginOptions(), + globals.isDevelopment + ? new ZoomMapPluginOptions() + : new DummyMapPluginOptions(), new MarkerLayerOptions( markers: buildMarkers( this.location, this.fires, this.industries, this.falsePos), @@ -170,47 +177,27 @@ class _LeafletMapState extends State { List buildMarkers(BasicLocation yourLocation, List fires, List falsePos, List industries) { List markers = []; - markers.add(buildMarker(yourLocation, MarkType.position)); + const calibrate = false; // useful when we change the fire icons size + markers.add(FireMarker(yourLocation, FireMarkType.position)); + if (calibrate) markers.add(FireMarker(yourLocation, FireMarkType.pixel)); falsePos.forEach((fire) { var coords = fire['geo']['coordinates']; var loc = BasicLocation(lon: coords[0], lat: coords[1]); - markers.add(buildMarker(loc, MarkType.falsePos)); + markers.add(FireMarker(loc, FireMarkType.falsePos)); + if (calibrate) markers.add(FireMarker(loc, FireMarkType.pixel)); }); industries.forEach((fire) { // print(fire['geo']['coordinates']); var coords = fire['geo']['coordinates']; var loc = BasicLocation(lon: coords[0], lat: coords[1]); - markers.add(buildMarker(loc, MarkType.industry)); + markers.add(FireMarker(loc, FireMarkType.industry)); + if (calibrate) markers.add(FireMarker(loc, FireMarkType.pixel)); }); fires.forEach((fire) { var loc = new BasicLocation(lat: fire['lat'], lon: fire['lon']); - markers.add(buildMarker(loc, MarkType.fire)); + markers.add(FireMarker(loc, FireMarkType.fire)); + markers.add(FireMarker(loc, FireMarkType.pixel)); }); return markers; } - - Marker buildMarker(location, MarkType type) { - return new Marker( - width: 80.0, - height: 80.0, - point: new LatLng(location.lat, location.lon), - builder: (ctx) => new Container( - child: buildImageMark(type), - ), - ); - } - - Widget buildImageMark(MarkType type) { - switch (type) { - case MarkType.position: - return new Icon(Icons.location_on, color: fires600, size: 50.0); - case MarkType.fire: - return new Image.asset('images/fire-marker-l.png'); - case MarkType.industry: - return new Image.asset('images/industry-marker-reg.png'); - case MarkType.falsePos: - default: - return new Image.asset('images/industry-marker.png'); - } - } } diff --git a/lib/globals.dart b/lib/globals.dart index c573976..8514d1a 100644 --- a/lib/globals.dart +++ b/lib/globals.dart @@ -8,11 +8,12 @@ import 'package:flutter/material.dart'; String gmapKey; String firesApiKey; String firesApiUrl; -String appName = 'All Against The Fire!'; -String appVersion = '0.0.1'; -String appLicense = "(c) 2017-2018 Comunes Association under the GNU Affero GPL v3"; -Widget appMediumIcon = Image.asset('images/logo-200.png', width: 60.0, height: 60.0); -Widget appIcon = Image.asset('images/logo-200.png', width: 24.0, height: 24.0); -Future prefs = SharedPreferences.getInstance(); +final String appName = 'All Against The Fire!'; +final String appVersion = '0.0.1'; +final String appLicense = "(c) 2017-2018 Comunes Association under the GNU Affero GPL v3"; +final Widget appMediumIcon = Image.asset('images/logo-200.png', width: 60.0, height: 60.0); +final Widget appIcon = Image.asset('images/logo-200.png', width: 24.0, height: 24.0); +final Future prefs = SharedPreferences.getInstance(); final List yourLocations = []; final GetIt getIt = new GetIt(); +final bool isDevelopment = true; diff --git a/lib/mainDrawer.dart b/lib/mainDrawer.dart index 466f16a..c7d3731 100644 --- a/lib/mainDrawer.dart +++ b/lib/mainDrawer.dart @@ -2,7 +2,6 @@ import 'package:flutter/material.dart'; import 'colors.dart'; import 'sandbox.dart'; -import 'introPage.dart'; import 'activeFires.dart'; import 'globals.dart' as globals; diff --git a/lib/customMapPlugin.dart b/lib/zoomMapPlugin.dart similarity index 100% rename from lib/customMapPlugin.dart rename to lib/zoomMapPlugin.dart