@@ -32,20 +32,14 @@ class OfflineMapWidgetState extends State<OfflineMapWidget> {
32
32
final StreamController <double > _tileRegionLoadProgress =
33
33
StreamController .broadcast ();
34
34
35
- _onMapCreated (MapboxMap mapboxMap) async {
36
- this .mapboxMap = mapboxMap;
37
- await _downloadStylePack ();
38
- await _downloadTileRegion ();
39
- }
40
-
41
35
_downloadStylePack () async {
42
36
final offlineManager = await OfflineManager .create ();
43
37
final stylePackLoadOptions = StylePackLoadOptions (
44
38
glyphsRasterizationMode:
45
39
GlyphsRasterizationMode .IDEOGRAPHS_RASTERIZED_LOCALLY ,
46
40
metadata: {"tag" : "test" },
47
41
acceptExpired: false );
48
- offlineManager.loadStylePack (MapboxStyles .OUTDOORS , stylePackLoadOptions,
42
+ offlineManager.loadStylePack (MapboxStyles .SATELLITE_STREETS , stylePackLoadOptions,
49
43
(progress) {
50
44
final percentage =
51
45
progress.completedResourceCount / progress.requiredResourceCount;
@@ -59,13 +53,15 @@ class OfflineMapWidgetState extends State<OfflineMapWidget> {
59
53
}
60
54
61
55
_downloadTileRegion () async {
62
- final tmpDir = await getTemporaryDirectory ();
63
- final tileStore = await TileStore .createAt (await tmpDir .uri);
56
+ final path = await getTemporaryDirectory ();
57
+ final tileStore = await TileStore .createAt (path .uri);
64
58
final tileRegionLoadOptions = TileRegionLoadOptions (
65
- geometry: Point (coordinates : Position ( - 80.1263 , 25.7845 )) .toJson (),
59
+ geometry: City .helsinki .toJson (),
66
60
descriptorsOptions: [
61
+ // If you are using a raster tileset you may need to set a different pixelRatio.
62
+ // The default is UIScreen.main.scale on iOS and displayMetrics's density on Android.
67
63
TilesetDescriptorOptions (
68
- styleURI: MapboxStyles .OUTDOORS , minZoom: 0 , maxZoom: 16 )
64
+ styleURI: MapboxStyles .SATELLITE_STREETS , minZoom: 0 , maxZoom: 16 )
69
65
],
70
66
acceptExpired: true ,
71
67
networkRestriction: NetworkRestriction .NONE );
@@ -85,18 +81,40 @@ class OfflineMapWidgetState extends State<OfflineMapWidget> {
85
81
86
82
@override
87
83
Widget build (BuildContext context) {
88
- final mapWidget = MapWidget (
89
- key : ValueKey ( "mapWidget" ),
90
- styleUri : MapboxStyles . OUTDOORS ,
91
- cameraOptions : CameraOptions (center : City .helsinki, zoom : 2.0 ),
92
- onMapCreated : _onMapCreated,
93
- );
84
+ String downloadButtonText = "Download Map" ;
85
+ final mapIsDownloaded = Future
86
+ . wait ([_tileRegionLoadProgress.sink.done, _stylePackProgress.sink.done])
87
+ . whenComplete (() async {
88
+ await OfflineSwitch .shared. setMapboxStackConnected ( false );
89
+ } );
94
90
95
91
return new Column (
96
92
mainAxisSize: MainAxisSize .min,
97
93
children: [
98
94
Expanded (
99
- child: mapWidget,
95
+ child: FutureBuilder (future: mapIsDownloaded, builder: (context, snapshot) {
96
+ if (snapshot.hasData) {
97
+ return MapWidget (
98
+ key: ValueKey ("mapWidget" ),
99
+ styleUri: MapboxStyles .SATELLITE_STREETS ,
100
+ cameraOptions: CameraOptions (center: City .helsinki, zoom: 12.0 ),
101
+ );
102
+ } else {
103
+ return TextButton (
104
+ style: ButtonStyle (
105
+ foregroundColor: MaterialStateProperty .all <Color >(Colors .blue),
106
+ ),
107
+ onPressed: () async {
108
+ setState (() {
109
+ downloadButtonText = "Downloading" ;
110
+ });
111
+ await _downloadStylePack ();
112
+ await _downloadTileRegion ();
113
+ },
114
+ child: Text (downloadButtonText),
115
+ );
116
+ }
117
+ }),
100
118
),
101
119
SizedBox (
102
120
height: 100 ,
0 commit comments