This repository has been archived by the owner on Nov 7, 2024. It is now read-only.
forked from mapbox/mapbox-maps-flutter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolyline_annotations.dart
196 lines (174 loc) · 5.42 KB
/
polyline_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
191
192
193
194
195
196
import 'package:flutter/material.dart';
import 'package:mapbox_maps_flutter/mapbox_maps_flutter.dart';
import 'package:mapbox_maps_example/utils.dart';
import 'package:turf/helpers.dart';
import 'main.dart';
import 'page.dart';
class PolylineAnnotationPage extends ExamplePage {
PolylineAnnotationPage()
: super(const Icon(Icons.map), 'Polyline Annotations');
@override
Widget build(BuildContext context) {
return const PolylineAnnotationPageBody();
}
}
class AnnotationClickListener extends OnPolylineAnnotationClickListener {
@override
void onPolylineAnnotationClick(PolylineAnnotation annotation) {
print("onAnnotationClick, id: ${annotation.id}");
}
}
class PolylineAnnotationPageBody extends StatefulWidget {
const PolylineAnnotationPageBody();
@override
State<StatefulWidget> createState() => PolylineAnnotationPageBodyState();
}
class PolylineAnnotationPageBodyState
extends State<PolylineAnnotationPageBody> {
PolylineAnnotationPageBodyState();
MapboxMap? mapboxMap;
PolylineAnnotation? polylineAnnotation;
PolylineAnnotationManager? polylineAnnotationManager;
int styleIndex = 1;
_onMapCreated(MapboxMap mapboxMap) {
this.mapboxMap = mapboxMap;
mapboxMap.annotations.createPolylineAnnotationManager().then((value) {
polylineAnnotationManager = value;
createOneAnnotation();
final positions = <List<Position>>[];
for (int i = 0; i < 99; i++) {
positions.add(createRandomPositionList());
}
polylineAnnotationManager?.createMulti(positions
.map((e) => PolylineAnnotationOptions(
geometry: LineString(coordinates: e).toJson(),
lineColor: createRandomColor()))
.toList());
polylineAnnotationManager
?.addOnPolylineAnnotationClickListener(AnnotationClickListener());
});
}
@override
void initState() {
super.initState();
}
@override
void dispose() {
super.dispose();
}
Widget _update() {
return TextButton(
child: Text('update a polyline annotation'),
onPressed: () {
if (polylineAnnotation != null) {
var lineString =
LineString.fromJson((polylineAnnotation!.geometry)!.cast());
var newlineString = LineString(
coordinates: lineString.coordinates
.map((e) => Position(e.lng + 1.0, e.lat + 1.0))
.toList());
polylineAnnotation?.geometry = newlineString.toJson();
polylineAnnotationManager?.update(polylineAnnotation!);
}
},
);
}
Widget _create() {
return TextButton(
child: Text('create a polyline annotation'),
onPressed: () {
createOneAnnotation();
},
);
}
void createOneAnnotation() {
polylineAnnotationManager
?.create(PolylineAnnotationOptions(
geometry: LineString(coordinates: [
Position(
1.0,
2.0,
),
Position(
10.0,
20.0,
)
]).toJson(),
lineColor: Colors.red.value,
lineWidth: 2))
.then((value) => polylineAnnotation = value);
}
Widget _delete() {
return TextButton(
child: Text('delete a polyline annotation'),
onPressed: () {
if (polylineAnnotation != null) {
polylineAnnotationManager?.delete(polylineAnnotation!);
}
},
);
}
Widget _deleteAll() {
return TextButton(
child: Text('delete all polyline annotations'),
onPressed: () {
polylineAnnotationManager?.deleteAll();
},
);
}
@override
Widget build(BuildContext context) {
final MapWidget mapWidget = MapWidget(
key: ValueKey("mapWidget"),
resourceOptions: ResourceOptions(accessToken: MapsDemo.ACCESS_TOKEN),
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 (polylineAnnotationManager != null) {
mapboxMap?.annotations
.removeAnnotationManager(polylineAnnotationManager!);
polylineAnnotationManager = null;
}
}),
],
),
),
body: colmn);
}
}