forked from mapbox/mapbox-maps-flutter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
polygon_annotations.dart
190 lines (169 loc) · 5.43 KB
/
polygon_annotations.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import 'package:flutter/material.dart';
import 'package:mapbox_maps_flutter/mapbox_maps_flutter.dart';
import 'package:mapbox_maps_example/utils.dart';
import 'page.dart';
class PolygonAnnotationPage extends ExamplePage {
PolygonAnnotationPage() : super(const Icon(Icons.map), 'Polygon Annotations');
@override
Widget build(BuildContext context) {
return const PolygonAnnotationPageBody();
}
}
class PolygonAnnotationPageBody extends StatefulWidget {
const PolygonAnnotationPageBody();
@override
State<StatefulWidget> createState() => PolygonAnnotationPageBodyState();
}
class AnnotationClickListener extends OnPolygonAnnotationClickListener {
@override
void onPolygonAnnotationClick(PolygonAnnotation annotation) {
print("onAnnotationClick, id: ${annotation.id}");
}
}
class PolygonAnnotationPageBodyState extends State<PolygonAnnotationPageBody> {
PolygonAnnotationPageBodyState();
MapboxMap? mapboxMap;
PolygonAnnotation? polygonAnnotation;
PolygonAnnotationManager? polygonAnnotationManager;
int styleIndex = 1;
_onMapCreated(MapboxMap mapboxMap) {
this.mapboxMap = mapboxMap;
mapboxMap.setCamera(CameraOptions(
center: Point(coordinates: Position(-3.363937, -10.733102)),
zoom: 1,
pitch: 0));
mapboxMap.annotations.createPolygonAnnotationManager().then((value) {
polygonAnnotationManager = value;
createOneAnnotation();
var options = <PolygonAnnotationOptions>[];
for (var i = 0; i < 2; i++) {
options.add(PolygonAnnotationOptions(
geometry:
Polygon(coordinates: createRandomPositionsList()).toJson(),
fillColor: createRandomColor()));
}
polygonAnnotationManager?.createMulti(options);
polygonAnnotationManager
?.addOnPolygonAnnotationClickListener(AnnotationClickListener());
});
}
void createOneAnnotation() {
polygonAnnotationManager
?.create(PolygonAnnotationOptions(
geometry: Polygon(coordinates: [
[
Position(-3.363937, -10.733102),
Position(1.754703, -19.716317),
Position(-15.747196, -21.085074),
Position(-3.363937, -10.733102)
]
]).toJson(),
fillColor: Colors.red.value,
fillOutlineColor: Colors.purple.value))
.then((value) => polygonAnnotation = value);
}
@override
void initState() {
super.initState();
}
@override
void dispose() {
super.dispose();
}
Widget _update() {
return TextButton(
child: Text('update a polygon annotation'),
onPressed: () {
if (polygonAnnotation != null) {
var polygon = Polygon.fromJson((polygonAnnotation!.geometry)!.cast());
var newPolygon = Polygon(
coordinates: polygon.coordinates
.map((e) =>
e.map((e) => Position(e.lng + 1.0, e.lat + 1.0)).toList())
.toList());
polygonAnnotation?.geometry = newPolygon.toJson();
polygonAnnotationManager?.update(polygonAnnotation!);
}
},
);
}
Widget _create() {
return TextButton(
child: Text('create a polygon annotation'),
onPressed: () {
createOneAnnotation();
},
);
}
Widget _delete() {
return TextButton(
child: Text('delete a polygon annotation'),
onPressed: () {
if (polygonAnnotation != null) {
polygonAnnotationManager?.delete(polygonAnnotation!);
}
},
);
}
Widget _deleteAll() {
return TextButton(
child: Text('delete all polygon annotations'),
onPressed: () {
polygonAnnotationManager?.deleteAll();
},
);
}
@override
Widget build(BuildContext context) {
final MapWidget mapWidget =
MapWidget(key: ValueKey("mapWidget"), onMapCreated: _onMapCreated);
final List<Widget> listViewChildren = <Widget>[];
listViewChildren.addAll(
<Widget>[_create(), _update(), _delete(), _deleteAll()],
);
final colmn = Column(
mainAxisSize: MainAxisSize.min,
children: [
Center(
child: SizedBox(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height - 400,
child: mapWidget),
),
Expanded(
child: ListView(
children: listViewChildren,
),
)
],
);
return Scaffold(
floatingActionButton: Padding(
padding: const EdgeInsets.all(12.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
FloatingActionButton(
child: Icon(Icons.swap_horiz),
heroTag: null,
onPressed: () {
mapboxMap?.style.setStyleURI(annotationStyles[
++styleIndex % annotationStyles.length]);
}),
SizedBox(height: 10),
FloatingActionButton(
child: Icon(Icons.clear),
heroTag: null,
onPressed: () {
if (polygonAnnotationManager != null) {
mapboxMap?.annotations
.removeAnnotationManager(polygonAnnotationManager!);
polygonAnnotationManager = null;
}
}),
],
),
),
body: colmn);
}
}