forked from mapbox/mapbox-maps-flutter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
circle_annotations.dart
184 lines (162 loc) · 5.07 KB
/
circle_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
import 'package:flutter/material.dart' hide Visibility;
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 CircleAnnotationPage extends ExamplePage {
CircleAnnotationPage() : super(const Icon(Icons.map), 'Circle Annotations');
@override
Widget build(BuildContext context) {
return const CircleAnnotationPageBody();
}
}
class CircleAnnotationPageBody extends StatefulWidget {
const CircleAnnotationPageBody();
@override
State<StatefulWidget> createState() => CircleAnnotationPageBodyState();
}
class AnnotationClickListener extends OnCircleAnnotationClickListener {
@override
void onCircleAnnotationClick(CircleAnnotation annotation) {
print("onAnnotationClick, id: ${annotation.id}");
}
}
class CircleAnnotationPageBodyState extends State<CircleAnnotationPageBody> {
CircleAnnotationPageBodyState();
MapboxMap? mapboxMap;
CircleAnnotation? circleAnnotation;
CircleAnnotationManager? circleAnnotationManager;
int styleIndex = 1;
_onMapCreated(MapboxMap mapboxMap) {
this.mapboxMap = mapboxMap;
mapboxMap.annotations.createCircleAnnotationManager().then((value) {
circleAnnotationManager = value;
createOneAnnotation();
var options = <CircleAnnotationOptions>[];
for (var i = 0; i < 2000; i++) {
options.add(CircleAnnotationOptions(
geometry: createRandomPoint().toJson(),
circleColor: createRandomColor(),
circleRadius: 8.0));
}
circleAnnotationManager?.createMulti(options);
circleAnnotationManager
?.addOnCircleAnnotationClickListener(AnnotationClickListener());
});
}
void createOneAnnotation() {
circleAnnotationManager?.create(CircleAnnotationOptions(
geometry: Point(
coordinates: Position(
0.381457,
6.687337,
)).toJson(),
circleColor: Colors.yellow.value,
circleRadius: 12.0,
));
}
@override
void initState() {
super.initState();
}
@override
void dispose() {
super.dispose();
}
Widget _update() {
return TextButton(
child: Text('update a circle annotation'),
onPressed: () {
if (circleAnnotation != null) {
var point = Point.fromJson((circleAnnotation!.geometry)!.cast());
var newPoint = Point(
coordinates: Position(
point.coordinates.lng + 1.0, point.coordinates.lat + 1.0));
circleAnnotation?.geometry = newPoint.toJson();
circleAnnotationManager?.update(circleAnnotation!);
}
},
);
}
Widget _create() {
return TextButton(
child: Text('create a circle annotation'),
onPressed: () async {
createOneAnnotation();
},
);
}
Widget _delete() {
return TextButton(
child: Text('delete a circle annotation'),
onPressed: () async {
if (circleAnnotation != null) {
circleAnnotationManager?.delete(circleAnnotation!);
}
});
}
Widget _deleteAll() {
return TextButton(
child: Text('delete all circle annotations'),
onPressed: () {
circleAnnotationManager?.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 (circleAnnotationManager != null) {
mapboxMap?.annotations
.removeAnnotationManager(circleAnnotationManager!);
circleAnnotationManager = null;
}
}),
],
),
),
body: colmn);
}
}