From 6074e017754461f64098367ecd15bae16eae3eb5 Mon Sep 17 00:00:00 2001 From: Vahid Sohrabloo Date: Mon, 5 Mar 2018 13:58:49 +0330 Subject: [PATCH 1/2] Add Polyline --- flutter_map/lib/flutter_map.dart | 4 + flutter_map/lib/src/layer/polyline_layer.dart | 84 +++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 flutter_map/lib/src/layer/polyline_layer.dart diff --git a/flutter_map/lib/flutter_map.dart b/flutter_map/lib/flutter_map.dart index e4bfa8dc9..ee72beaac 100644 --- a/flutter_map/lib/flutter_map.dart +++ b/flutter_map/lib/flutter_map.dart @@ -10,6 +10,7 @@ import 'package:flutter_map/src/map/map.dart'; export 'src/layer/layer.dart'; export 'src/layer/tile_layer.dart'; export 'src/layer/marker_layer.dart'; +export 'src/layer/polyline_layer.dart'; export 'src/map/map.dart'; class FlutterMap extends StatefulWidget { @@ -58,6 +59,9 @@ class _FlutterMapState extends State if (options is MarkerLayerOptions) { return new MarkerLayer(options, mapState); } + if (options is PolylineLayerOptions) { + return new PolylineLayer(options,mapState);; + } return null; } } diff --git a/flutter_map/lib/src/layer/polyline_layer.dart b/flutter_map/lib/src/layer/polyline_layer.dart new file mode 100644 index 000000000..854598a0d --- /dev/null +++ b/flutter_map/lib/src/layer/polyline_layer.dart @@ -0,0 +1,84 @@ +import 'package:flutter/widgets.dart'; +import 'package:latlong/latlong.dart'; +import 'package:flutter_map/flutter_map.dart'; +import 'dart:ui'; + + +class PolylineLayerOptions extends LayerOptions { + final List polylines; + PolylineLayerOptions({this.polylines = const []}); +} + +class Polyline { + final List points; + List offsets; + final double strokeWidth; + final Color color; + Polyline({ + this.points, + this.strokeWidth = 1.0, + this.color = const Color(0xFF00FF00), + }); +} + +class PolylineLayer extends StatelessWidget { + final PolylineLayerOptions polylineOpts; + final MapState map; + Offset _offset = new Offset(73.71875271111094, 231.37884957158894); + GlobalKey _paintKey = new GlobalKey(); + + PolylineLayer(this.polylineOpts, this.map); + + Widget build(BuildContext context) { + map.onMoved.listen((int input) { + for (var polylineOpt in this.polylineOpts.polylines) { + polylineOpt.offsets = []; + var i = 0; + for (var point in polylineOpt.points) { + i++; + var pos = map.project(point); + pos = pos.multiplyBy(map.getZoomScale(map.zoom, map.zoom)) - + map.getPixelOrigin(); + polylineOpt.offsets.add(new Offset(pos.x, pos.y)); + if (i != 1 && i != polylineOpt.points.length) { + polylineOpt.offsets.add(new Offset(pos.x, pos.y)); + } + } + } + }); + var polylines = []; + + for (var polylineOpt in this.polylineOpts.polylines) { + polylines.add( + new CustomPaint( + key: _paintKey, + painter: new PolylinePainter(polylineOpt), + ), + ); + } + return new Container( + child: new Stack( + children: polylines, + ), + ); + } +} + +class PolylinePainter extends CustomPainter { + final Polyline polylineOpt; + PolylinePainter(this.polylineOpt); + + @override + void paint(Canvas canvas, Size size) { + if(polylineOpt.offsets==null){ + return; + } + final Paint paint = new Paint()..color = polylineOpt.color; + paint.strokeWidth = polylineOpt.strokeWidth; + canvas.drawPoints(PointMode.lines, polylineOpt.offsets, paint); + } + + @override + bool shouldRepaint(PolylinePainter other) => + other.polylineOpt.offsets != polylineOpt.offsets; +} From da402a54e472764a0c3a20a1d5741db242cdddba Mon Sep 17 00:00:00 2001 From: Vahid Sohrabloo Date: Wed, 7 Mar 2018 18:39:58 +0330 Subject: [PATCH 2/2] Use StreamBuilder for Polyline --- flutter_map/lib/src/layer/polyline_layer.dart | 66 +++++++++---------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/flutter_map/lib/src/layer/polyline_layer.dart b/flutter_map/lib/src/layer/polyline_layer.dart index 854598a0d..6f9d51c4a 100644 --- a/flutter_map/lib/src/layer/polyline_layer.dart +++ b/flutter_map/lib/src/layer/polyline_layer.dart @@ -24,42 +24,43 @@ class Polyline { class PolylineLayer extends StatelessWidget { final PolylineLayerOptions polylineOpts; final MapState map; - Offset _offset = new Offset(73.71875271111094, 231.37884957158894); - GlobalKey _paintKey = new GlobalKey(); - PolylineLayer(this.polylineOpts, this.map); Widget build(BuildContext context) { - map.onMoved.listen((int input) { - for (var polylineOpt in this.polylineOpts.polylines) { - polylineOpt.offsets = []; - var i = 0; - for (var point in polylineOpt.points) { - i++; - var pos = map.project(point); - pos = pos.multiplyBy(map.getZoomScale(map.zoom, map.zoom)) - - map.getPixelOrigin(); - polylineOpt.offsets.add(new Offset(pos.x, pos.y)); - if (i != 1 && i != polylineOpt.points.length) { + return new StreamBuilder( + stream: map.onMoved, // a Stream or null + builder: (BuildContext context, AsyncSnapshot snapshot) { + for (var polylineOpt in this.polylineOpts.polylines) { + polylineOpt.offsets = []; + var i = 0; + for (var point in polylineOpt.points) { + i++; + var pos = map.project(point); + pos = pos.multiplyBy(map.getZoomScale(map.zoom, map.zoom)) - + map.getPixelOrigin(); polylineOpt.offsets.add(new Offset(pos.x, pos.y)); + if (i != 1 && i != polylineOpt.points.length) { + polylineOpt.offsets.add(new Offset(pos.x, pos.y)); + } } } - } - }); - var polylines = []; - - for (var polylineOpt in this.polylineOpts.polylines) { - polylines.add( - new CustomPaint( - key: _paintKey, - painter: new PolylinePainter(polylineOpt), - ), - ); - } - return new Container( - child: new Stack( - children: polylines, - ), + var polylines = []; + for (var polylineOpt in this.polylineOpts.polylines) { + polylines.add( + new CustomPaint( + painter: new PolylinePainter(polylineOpt), + child: new Container( + child: new Text( + " "), + )), + ); + } + return new Container( + child: new Stack( + children: polylines, + ), + ); + }, ); } } @@ -70,7 +71,7 @@ class PolylinePainter extends CustomPainter { @override void paint(Canvas canvas, Size size) { - if(polylineOpt.offsets==null){ + if (polylineOpt.offsets == null) { return; } final Paint paint = new Paint()..color = polylineOpt.color; @@ -79,6 +80,5 @@ class PolylinePainter extends CustomPainter { } @override - bool shouldRepaint(PolylinePainter other) => - other.polylineOpt.offsets != polylineOpt.offsets; + bool shouldRepaint(PolylinePainter other) => false; }