Skip to content

Commit f39eaa7

Browse files
authored
feat!: use FeatureCollection in Layers (#353)
This pr allows to add properties. Still looking into improving the API design somehow, suggestions welcome.
1 parent c5d7060 commit f39eaa7

14 files changed

+138
-116
lines changed

example/lib/layers_circle_page.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ class LayersCirclePage extends StatefulWidget {
1212
}
1313

1414
class _LayersCirclePageState extends State<LayersCirclePage> {
15-
final _points = <Point>[
16-
const Point(Geographic(lon: 9.17, lat: 47.68)),
17-
const Point(Geographic(lon: 9.17, lat: 48)),
18-
const Point(Geographic(lon: 9, lat: 48)),
19-
const Point(Geographic(lon: 9.5, lat: 48)),
15+
final _points = <Feature<Point>>[
16+
const Feature(geometry: Point(Geographic(lon: 9.17, lat: 47.68))),
17+
const Feature(geometry: Point(Geographic(lon: 9.17, lat: 48))),
18+
const Feature(geometry: Point(Geographic(lon: 9, lat: 48))),
19+
const Feature(geometry: Point(Geographic(lon: 9.5, lat: 48))),
2020
];
2121

2222
@override
@@ -31,7 +31,7 @@ class _LayersCirclePageState extends State<LayersCirclePage> {
3131
onEvent: (event) {
3232
if (event case MapEventClick()) {
3333
setState(() {
34-
_points.add(Point(event.point));
34+
_points.add(Feature(geometry: Point(event.point)));
3535
});
3636
}
3737
},

example/lib/layers_marker_page.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ class LayersMarkerPage extends StatefulWidget {
1414
}
1515

1616
class _LayersMarkerPageState extends State<LayersMarkerPage> {
17-
final _points = <Point>[
18-
const Point(Geographic(lon: 9.17, lat: 47.68)),
19-
const Point(Geographic(lon: 9.17, lat: 48)),
20-
const Point(Geographic(lon: 9, lat: 48)),
21-
const Point(Geographic(lon: 9.5, lat: 48)),
17+
final _points = <Feature<Point>>[
18+
const Feature(geometry: Point(Geographic(lon: 9.17, lat: 47.68))),
19+
const Feature(geometry: Point(Geographic(lon: 9.17, lat: 48))),
20+
const Feature(geometry: Point(Geographic(lon: 9, lat: 48))),
21+
const Feature(geometry: Point(Geographic(lon: 9.5, lat: 48))),
2222
];
2323

2424
bool _imageLoaded = false;
@@ -47,7 +47,7 @@ class _LayersMarkerPageState extends State<LayersMarkerPage> {
4747
case MapEventClick():
4848
// add a new marker on click
4949
setState(() {
50-
_points.add(Point(event.point));
50+
_points.add(Feature(geometry: Point(event.point)));
5151
});
5252
default:
5353
// ignore all other events

example/lib/layers_mixed_page.dart

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ class LayersMixedPage extends StatefulWidget {
1515

1616
class _LayersMixedPageState extends State<LayersMixedPage> {
1717
final _random = Random.secure();
18-
final _circlePoints = <Point>[
19-
const Point(Geographic(lon: 9.17, lat: 47.68)),
20-
const Point(Geographic(lon: 9.17, lat: 48)),
21-
const Point(Geographic(lon: 9, lat: 48)),
22-
const Point(Geographic(lon: 9.5, lat: 48)),
18+
final _circlePoints = <Feature<Point>>[
19+
const Feature(geometry: Point(Geographic(lon: 9.17, lat: 47.68))),
20+
const Feature(geometry: Point(Geographic(lon: 9.17, lat: 48))),
21+
const Feature(geometry: Point(Geographic(lon: 9, lat: 48))),
22+
const Feature(geometry: Point(Geographic(lon: 9.5, lat: 48))),
2323
];
2424
Color _circleColor = Colors.orange.withValues(alpha: 0.5);
2525
PolylineLayer? _polylineLayer;
@@ -56,12 +56,14 @@ class _LayersMixedPageState extends State<LayersMixedPage> {
5656
if (_polylineLayer == null) {
5757
_polylineLayer = PolylineLayer(
5858
polylines: [
59-
LineString.from(
60-
const [
61-
Geographic(lon: 9.17, lat: 47.68),
62-
Geographic(lon: 9.5, lat: 48),
63-
Geographic(lon: 9, lat: 48),
64-
],
59+
Feature(
60+
geometry: LineString.from(
61+
const [
62+
Geographic(lon: 9.17, lat: 47.68),
63+
Geographic(lon: 9.5, lat: 48),
64+
Geographic(lon: 9, lat: 48),
65+
],
66+
),
6567
),
6668
],
6769
);
@@ -87,7 +89,7 @@ class _LayersMixedPageState extends State<LayersMixedPage> {
8789
onEvent: (event) {
8890
if (event case MapEventClick()) {
8991
setState(() {
90-
_circlePoints.add(Point(event.point));
92+
_circlePoints.add(Feature(geometry: Point(event.point)));
9193
});
9294
}
9395
},

example/lib/layers_polygon_page.dart

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,20 @@ class LayersPolygonPage extends StatefulWidget {
1212
}
1313

1414
class _LayersPolygonPageState extends State<LayersPolygonPage> {
15-
final _polygons = <Polygon>[
16-
Polygon.from(
17-
const [
18-
[
19-
Geographic(lon: 8.201306116882563, lat: 48.107357488669464),
20-
Geographic(lon: 8.885254895692924, lat: 48.09428546381665),
21-
Geographic(lon: 8.759684141159909, lat: 47.69326800157776),
22-
Geographic(lon: 9.631980099303235, lat: 48.08929468133098),
23-
Geographic(lon: 8.68543348810175, lat: 48.45383566718806),
24-
Geographic(lon: 8.201306116882563, lat: 48.107357488669464),
15+
final _polygons = <Feature<Polygon>>[
16+
Feature(
17+
geometry: Polygon.from(
18+
const [
19+
[
20+
Geographic(lon: 8.201306116882563, lat: 48.107357488669464),
21+
Geographic(lon: 8.885254895692924, lat: 48.09428546381665),
22+
Geographic(lon: 8.759684141159909, lat: 47.69326800157776),
23+
Geographic(lon: 9.631980099303235, lat: 48.08929468133098),
24+
Geographic(lon: 8.68543348810175, lat: 48.45383566718806),
25+
Geographic(lon: 8.201306116882563, lat: 48.107357488669464),
26+
],
2527
],
26-
],
28+
),
2729
),
2830
];
2931

@@ -38,11 +40,13 @@ class _LayersPolygonPageState extends State<LayersPolygonPage> {
3840
),
3941
onEvent: (event) {
4042
if (event case MapEventClick()) {
41-
final points = _polygons.first.rings.first.inserted(0, [
43+
final points = _polygons.first.geometry!.rings.first.inserted(0, [
4244
event.point,
4345
]);
4446
setState(() {
45-
_polygons[0] = Polygon.from([points.positions]);
47+
_polygons[0] = Feature(
48+
geometry: Polygon.from([points.positions]),
49+
);
4650
});
4751
}
4852
},

example/lib/layers_polyline_page.dart

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ class LayersPolylinePage extends StatefulWidget {
1212
}
1313

1414
class _LayersPolylinePageState extends State<LayersPolylinePage> {
15-
final _polylines = <LineString>[
16-
LineString.from(
17-
const [
18-
Geographic(lon: 9.17, lat: 47.68),
19-
Geographic(lon: 9.5, lat: 48),
20-
Geographic(lon: 9, lat: 48),
21-
],
15+
final _polylines = <Feature<LineString>>[
16+
Feature(
17+
geometry: LineString.from(
18+
const [
19+
Geographic(lon: 9.17, lat: 47.68),
20+
Geographic(lon: 9.5, lat: 48),
21+
Geographic(lon: 9, lat: 48),
22+
],
23+
),
2224
),
2325
];
2426

@@ -34,9 +36,13 @@ class _LayersPolylinePageState extends State<LayersPolylinePage> {
3436
onEvent: (event) {
3537
if (event case MapEventClick()) {
3638
// add the clicked point to the line
37-
final newChain = _polylines.first.chain.inserted(0, [event.point]);
39+
final newChain = _polylines.first.geometry!.chain.inserted(0, [
40+
event.point,
41+
]);
3842
setState(() {
39-
_polylines[0] = LineString.from(newChain.positions);
43+
_polylines[0] = Feature(
44+
geometry: LineString.from(newChain.positions),
45+
);
4046
});
4147
}
4248
},

example/lib/offline_page.dart

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -336,29 +336,31 @@ class _OfflineMapPage extends StatelessWidget {
336336
color: Colors.red,
337337
width: 3,
338338
polylines: [
339-
LineString.from(
340-
[
341-
Geographic(
342-
lon: bounds.longitudeWest,
343-
lat: bounds.latitudeSouth,
344-
),
345-
Geographic(
346-
lon: bounds.longitudeWest,
347-
lat: bounds.latitudeNorth,
348-
),
349-
Geographic(
350-
lon: bounds.longitudeEast,
351-
lat: bounds.latitudeNorth,
352-
),
353-
Geographic(
354-
lon: bounds.longitudeEast,
355-
lat: bounds.latitudeSouth,
356-
),
357-
Geographic(
358-
lon: bounds.longitudeWest,
359-
lat: bounds.latitudeSouth,
360-
),
361-
],
339+
Feature(
340+
geometry: LineString.from(
341+
[
342+
Geographic(
343+
lon: bounds.longitudeWest,
344+
lat: bounds.latitudeSouth,
345+
),
346+
Geographic(
347+
lon: bounds.longitudeWest,
348+
lat: bounds.latitudeNorth,
349+
),
350+
Geographic(
351+
lon: bounds.longitudeEast,
352+
lat: bounds.latitudeNorth,
353+
),
354+
Geographic(
355+
lon: bounds.longitudeEast,
356+
lat: bounds.latitudeSouth,
357+
),
358+
Geographic(
359+
lon: bounds.longitudeWest,
360+
lat: bounds.latitudeSouth,
361+
),
362+
],
363+
),
362364
),
363365
],
364366
),

lib/src/layer/circle_layer.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ part of 'layer.dart';
44
///
55
/// {@category Layers}
66
@immutable
7-
class CircleLayer extends Layer<Point> {
7+
class CircleLayer extends Layer<Feature<Point>> {
88
/// Create a new [CircleLayer] instance.
99
const CircleLayer({
10-
required List<Point> points,
10+
required List<Feature<Point>> points,
1111
this.radius = 5,
1212
this.color = const Color(0xFF000000),
1313
this.blur = 0,

lib/src/layer/layer.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ part 'polyline_layer.dart';
1414
///
1515
/// {@category Layers}
1616
@immutable
17-
sealed class Layer<G extends Geometry> {
17+
sealed class Layer<G extends Feature<Geometry>> {
1818
const Layer._({required this.list});
1919

2020
/// The [List] of layers.

lib/src/layer/layer_manager.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class LayerManager {
1313
for (final (index, layer) in layers.indexed) {
1414
final source = GeoJsonSource(
1515
id: layer.getSourceId(index),
16-
data: GeometryCollection(layer.list).toText(),
16+
data: FeatureCollection(layer.list).toText(),
1717
);
1818
style.addSource(source);
1919
style.addLayer(layer.createStyleLayer(index));
@@ -40,12 +40,12 @@ class LayerManager {
4040
if (oldLayer case Layer()) {
4141
style.updateGeoJsonSource(
4242
id: layer.getSourceId(index),
43-
data: GeometryCollection(layer.list).toText(),
43+
data: FeatureCollection(layer.list).toText(),
4444
);
4545
} else {
4646
final source = GeoJsonSource(
4747
id: layer.getSourceId(index),
48-
data: GeometryCollection(layer.list).toText(),
48+
data: FeatureCollection(layer.list).toText(),
4949
);
5050
style.addSource(source);
5151
}

lib/src/layer/marker_layer.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ part of 'layer.dart';
44
///
55
/// {@category Layers}
66
@immutable
7-
class MarkerLayer extends Layer<Point> {
7+
class MarkerLayer extends Layer<Feature<Point>> {
88
/// Create a new [MarkerLayer] instance.
99
const MarkerLayer({
10-
required List<Point> points,
10+
required List<Feature<Point>> points,
1111
this.iconAllowOverlap = false,
1212
this.iconIgnorePlacement = false,
1313
this.iconOptional = false,

0 commit comments

Comments
 (0)