Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Polyline #9

Merged
merged 2 commits into from
Mar 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions flutter_map/lib/flutter_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -58,6 +59,9 @@ class _FlutterMapState extends State<FlutterMap>
if (options is MarkerLayerOptions) {
return new MarkerLayer(options, mapState);
}
if (options is PolylineLayerOptions) {
return new PolylineLayer(options,mapState);;
}
return null;
}
}
84 changes: 84 additions & 0 deletions flutter_map/lib/src/layer/polyline_layer.dart
Original file line number Diff line number Diff line change
@@ -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<Polyline> polylines;
PolylineLayerOptions({this.polylines = const []});
}

class Polyline {
final List<LatLng> points;
List<Offset> 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<int>(
stream: map.onMoved, // a Stream<int> or null
builder: (BuildContext context, AsyncSnapshot<int> 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 = <Widget>[];
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;
}