forked from mapbox/mapbox-maps-flutter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
location.dart
299 lines (266 loc) · 8.08 KB
/
location.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:mapbox_maps_flutter/mapbox_maps_flutter.dart';
import 'package:permission_handler/permission_handler.dart';
import 'main.dart';
import 'page.dart';
class LocationPage extends ExamplePage {
LocationPage() : super(const Icon(Icons.map), 'Location Component');
@override
Widget build(BuildContext context) {
return const LocationPageBody();
}
}
class LocationPageBody extends StatefulWidget {
const LocationPageBody();
@override
State<StatefulWidget> createState() => LocationPageBodyState();
}
class LocationPageBodyState extends State<LocationPageBody> {
LocationPageBodyState();
final colors = [Colors.amber, Colors.black, Colors.blue];
MapboxMap? mapboxMap;
int _accuracyColor = 0;
int _pulsingColor = 0;
int _accuracyBorderColor = 0;
double _puckScale = 1.0;
_onMapCreated(MapboxMap mapboxMap) {
this.mapboxMap = mapboxMap;
}
@override
void initState() {
super.initState();
}
@override
void dispose() {
super.dispose();
}
Widget _show() {
return TextButton(
child: Text('show location'),
onPressed: () {
mapboxMap?.location
.updateSettings(LocationComponentSettings(enabled: true));
},
);
}
Widget _hide() {
return TextButton(
child: Text('hide location'),
onPressed: () {
mapboxMap?.location
.updateSettings(LocationComponentSettings(enabled: false));
},
);
}
Widget _showBearing() {
return TextButton(
child: Text('show location bearing'),
onPressed: () {
mapboxMap?.location.updateSettings(
LocationComponentSettings(puckBearingEnabled: true));
},
);
}
Widget _hideBearing() {
return TextButton(
child: Text('hide location bearing'),
onPressed: () {
mapboxMap?.location.updateSettings(
LocationComponentSettings(puckBearingEnabled: false));
},
);
}
Widget _showPulsing() {
return TextButton(
child: Text('show pulsing'),
onPressed: () {
mapboxMap?.location
.updateSettings(LocationComponentSettings(pulsingEnabled: true));
},
);
}
Widget _hidePulsing() {
return TextButton(
child: Text('hide pulsing'),
onPressed: () {
mapboxMap?.location
.updateSettings(LocationComponentSettings(pulsingEnabled: false));
},
);
}
Widget _showAccuracy() {
return TextButton(
child: Text('show accuracy'),
onPressed: () {
mapboxMap?.location
.updateSettings(LocationComponentSettings(showAccuracyRing: true));
},
);
}
Widget _hideAccuracy() {
return TextButton(
child: Text('hide accuracy'),
onPressed: () {
mapboxMap?.location
.updateSettings(LocationComponentSettings(showAccuracyRing: false));
},
);
}
Widget _switchAccuracyBorderColor() {
return TextButton(
child: Text('switch accuracy border color'),
onPressed: () {
_accuracyBorderColor++;
_accuracyBorderColor %= colors.length;
mapboxMap?.location.updateSettings(LocationComponentSettings(
accuracyRingBorderColor: colors[_accuracyBorderColor].value));
},
);
}
Widget _switchAccuracyColor() {
return TextButton(
child: Text('switch accuracy color'),
onPressed: () {
_pulsingColor++;
_pulsingColor %= colors.length;
mapboxMap?.location.updateSettings(LocationComponentSettings(
accuracyRingColor: colors[_pulsingColor].value));
},
);
}
Widget _switchPulsingColor() {
return TextButton(
child: Text('switch pulsing color'),
onPressed: () {
_accuracyColor++;
_accuracyColor %= colors.length;
mapboxMap?.location.updateSettings(LocationComponentSettings(
pulsingColor: colors[_accuracyColor].value));
},
);
}
Widget _switchLocationPuck2D() {
return TextButton(
child: Text('switch to 2d puck'),
onPressed: () async {
final ByteData bytes =
await rootBundle.load('assets/symbols/custom-icon.png');
final Uint8List list = bytes.buffer.asUint8List();
mapboxMap?.location.updateSettings(LocationComponentSettings(
locationPuck:
LocationPuck(locationPuck2D: LocationPuck2D(topImage: list))));
},
);
}
Widget _switchLocationPuck3D() {
return TextButton(
child: Text('switch to 3d puck'),
onPressed: () {
mapboxMap?.location.updateSettings(LocationComponentSettings(
locationPuck: LocationPuck(
locationPuck3D: LocationPuck3D(
modelUri:
"https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Models/master/2.0/Duck/glTF-Embedded/Duck.gltf",
modelScale: [_puckScale, _puckScale, _puckScale]))));
},
);
}
Widget _switchPuckScale() {
return TextButton(
child: Text('scale 3d puck'),
onPressed: () {
_puckScale /= 2;
if (_puckScale < 0.1) {
_puckScale = 1.0;
}
print("Scale : $_puckScale");
mapboxMap?.location.updateSettings(LocationComponentSettings(
locationPuck: LocationPuck(
locationPuck3D: LocationPuck3D(
modelUri:
"https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Models/master/2.0/Duck/glTF-Embedded/Duck.gltf",
modelScale: [_puckScale, _puckScale, _puckScale]))));
},
);
}
Widget _getPermission() {
return TextButton(
child: Text('get location permission'),
onPressed: () async {
var status = await Permission.locationWhenInUse.request();
print("Location granted : $status");
},
);
}
Widget _getSettings() {
return TextButton(
child: Text('get settings'),
onPressed: () {
mapboxMap?.location.getSettings().then(
(value) => ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text("""
Location settings :
enabled : ${value.enabled},
puckBearingEnabled : ${value.puckBearingEnabled}
puckBearingSource : ${value.puckBearingSource}
pulsing : ${value.pulsingEnabled}
pulsing radius : ${value.pulsingMaxRadius}
pulsing color : ${value.pulsingColor}
accuracy : ${value.showAccuracyRing},
accuracy color : ${value.accuracyRingColor}
accuracyRingBorderColor : ${value.accuracyRingBorderColor}
"""
.trim()),
backgroundColor: Theme.of(context).primaryColor,
duration: Duration(seconds: 2),
)));
},
);
}
@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>[
_getPermission(),
_show(),
_hide(),
_switchLocationPuck2D(),
_switchLocationPuck3D(),
_switchPuckScale(),
_showBearing(),
_hideBearing(),
_showAccuracy(),
_hideAccuracy(),
_showPulsing(),
_hidePulsing(),
_switchAccuracyColor(),
_switchPulsingColor(),
_switchAccuracyBorderColor(),
_getSettings(),
],
);
return 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,
),
)
],
);
}
}