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..6f9d51c4a --- /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; + PolylineLayer(this.polylineOpts, this.map); + + Widget build(BuildContext context) { + 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( + painter: new PolylinePainter(polylineOpt), + child: new Container( + child: new Text( + " "), + )), + ); + } + 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) => false; +}