forked from mapbox/mapbox-maps-flutter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
point_annotations.dart
198 lines (177 loc) · 5.74 KB
/
point_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
197
198
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:mapbox_maps_flutter/mapbox_maps_flutter.dart';
import 'package:mapbox_maps_example/utils.dart';
import 'page.dart';
class PointAnnotationPage extends ExamplePage {
PointAnnotationPage() : super(const Icon(Icons.map), 'Point Annotations');
@override
Widget build(BuildContext context) {
return const PointAnnotationPageBody();
}
}
class PointAnnotationPageBody extends StatefulWidget {
const PointAnnotationPageBody();
@override
State<StatefulWidget> createState() => PointAnnotationPageBodyState();
}
class AnnotationClickListener extends OnPointAnnotationClickListener {
@override
void onPointAnnotationClick(PointAnnotation annotation) {
print("onAnnotationClick, id: ${annotation.id}");
}
}
class PointAnnotationPageBodyState extends State<PointAnnotationPageBody> {
PointAnnotationPageBodyState();
MapboxMap? mapboxMap;
PointAnnotation? pointAnnotation;
PointAnnotationManager? pointAnnotationManager;
int styleIndex = 1;
_onMapCreated(MapboxMap mapboxMap) {
this.mapboxMap = mapboxMap;
mapboxMap.setCamera(CameraOptions(
center: Point(coordinates: Position(0, 0)), zoom: 1, pitch: 0));
mapboxMap.annotations.createPointAnnotationManager().then((value) async {
pointAnnotationManager = value;
final ByteData bytes =
await rootBundle.load('assets/symbols/custom-icon.png');
final Uint8List list = bytes.buffer.asUint8List();
createOneAnnotation(list);
var options = <PointAnnotationOptions>[];
for (var i = 0; i < 5; i++) {
options.add(
PointAnnotationOptions(geometry: createRandomPoint(), image: list));
}
pointAnnotationManager?.createMulti(options);
var carOptions = <PointAnnotationOptions>[];
for (var i = 0; i < 20; i++) {
carOptions.add(PointAnnotationOptions(
geometry: createRandomPoint(), iconImage: "car-15"));
}
pointAnnotationManager?.createMulti(carOptions);
pointAnnotationManager
?.addOnPointAnnotationClickListener(AnnotationClickListener());
});
}
void createOneAnnotation(Uint8List list) {
pointAnnotationManager
?.create(PointAnnotationOptions(
geometry: Point(
coordinates: Position(
0.381457,
6.687337,
)),
textField: "custom-icon",
textOffset: [0.0, -2.0],
textColor: Colors.red.value,
iconSize: 1.3,
iconOffset: [0.0, -5.0],
symbolSortKey: 10,
image: list))
.then((value) => pointAnnotation = value);
}
@override
void initState() {
super.initState();
}
@override
void dispose() {
super.dispose();
}
Widget _update() {
return TextButton(
child: Text('update a point annotation'),
onPressed: () {
if (pointAnnotation != null) {
var point = pointAnnotation!.geometry;
var newPoint = Point(
coordinates: Position(
point.coordinates.lng + 1.0, point.coordinates.lat + 1.0));
pointAnnotation?.geometry = newPoint;
pointAnnotationManager?.update(pointAnnotation!);
}
},
);
}
Widget _create() {
return TextButton(
child: Text('create a point annotation'),
onPressed: () async {
final ByteData bytes =
await rootBundle.load('assets/symbols/custom-icon.png');
final Uint8List list = bytes.buffer.asUint8List();
createOneAnnotation(list);
});
}
Widget _delete() {
return TextButton(
child: Text('delete a point annotation'),
onPressed: () {
if (pointAnnotation != null) {
pointAnnotationManager?.delete(pointAnnotation!);
}
},
);
}
Widget _deleteAll() {
return TextButton(
child: Text('delete all point annotations'),
onPressed: () {
pointAnnotationManager?.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 (pointAnnotationManager != null) {
mapboxMap?.annotations
.removeAnnotationManager(pointAnnotationManager!);
pointAnnotationManager = null;
}
}),
],
),
),
body: colmn);
}
}