This repository was archived by the owner on Jun 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 186
Expand file tree
/
Copy pathstatic_map_provider.dart
More file actions
142 lines (128 loc) · 4.88 KB
/
Copy pathstatic_map_provider.dart
File metadata and controls
142 lines (128 loc) · 4.88 KB
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
import 'dart:async';
import 'package:map_view/location.dart';
import 'package:uri/uri.dart';
import 'map_view.dart';
import 'locations.dart';
import 'map_view_type.dart';
class StaticMapProvider {
final String googleMapsApiKey;
static const int defaultZoomLevel = 4;
static const int defaultWidth = 600;
static const int defaultHeight = 400;
static const StaticMapViewType defaultMaptype = StaticMapViewType.roadmap;
StaticMapProvider(this.googleMapsApiKey);
///
/// Creates a Uri for the Google Static Maps API
/// Centers the map on [center] using a zoom of [zoomLevel]
/// Specify a [width] and [height] that you would like the resulting image to be. The default is 600w x 400h
///
Uri getStaticUri(Location center, int zoomLevel,
{int width, int height, StaticMapViewType mapType}) {
return _buildUrl(
null,
center,
zoomLevel ?? defaultZoomLevel,
width ?? defaultWidth,
height ?? defaultHeight,
mapType ?? defaultMaptype);
}
///
/// Creates a Uri for the Google Static Maps API using a list of locations to create pins on the map
/// [locations] must have at least 1 location
/// Specify a [width] and [height] that you would like the resulting image to be. The default is 600w x 400h
///
Uri getStaticUriWithMarkers(List<Marker> markers,
{int width, int height, StaticMapViewType maptype, Location center}) {
return _buildUrl(markers, center, null, width ?? defaultWidth,
height ?? defaultHeight, maptype ?? defaultMaptype);
}
///
/// Creates a Uri for the Google Static Maps API using a list of locations to create pins on the map
/// [locations] must have at least 1 location
/// Specify a [width] and [height] that you would like the resulting image to be. The default is 600w x 400h
/// Centers the map on [center] using a zoom of [zoomLevel]
///
Uri getStaticUriWithMarkersAndZoom(List<Marker> markers,
{int width,
int height,
StaticMapViewType maptype,
Location center,
int zoomLevel}) {
return _buildUrl(markers, center, zoomLevel, width ?? defaultWidth,
height ?? defaultHeight, maptype ?? defaultMaptype);
}
///
/// Creates a Uri for the Google Static Maps API using an active MapView
/// This method is useful for generating a static image
/// [mapView] must currently be visible when you call this.
/// Specify a [width] and [height] that you would like the resulting image to be. The default is 600w x 400h
///
Future<Uri> getImageUriFromMap(MapView mapView,
{int width, int height, StaticMapViewType maptype}) async {
var markers = await mapView.visibleAnnotations;
var center = await mapView.centerLocation;
var zoom = await mapView.zoomLevel;
return _buildUrl(markers, center, zoom.toInt(), width ?? defaultWidth,
height ?? defaultHeight, maptype ?? defaultMaptype);
}
Uri _buildUrl(List<Marker> locations, Location center, int zoomLevel,
int width, int height, StaticMapViewType mapType) {
var finalUri = new UriBuilder()
..scheme = 'https'
..host = 'maps.googleapis.com'
..port = 443
..path = '/maps/api/staticmap';
if (center == null && (locations == null || locations.length == 0)) {
center = Locations.centerOfUSA;
}
if (locations == null || locations.length == 0) {
if (center == null) center = Locations.centerOfUSA;
finalUri.queryParameters = {
'center': '${center.latitude},${center.longitude}',
'zoom': zoomLevel.toString(),
'size': '${width ?? defaultWidth}x${height ?? defaultHeight}',
'maptype': _getMapTypeQueryParam(mapType),
'key': googleMapsApiKey,
};
} else {
List<String> markers = new List();
locations.forEach((location) {
num lat = location.latitude;
num lng = location.longitude;
String marker = '$lat,$lng';
markers.add(marker);
});
String markersString = markers.join('|');
finalUri.queryParameters = {
'markers': markersString,
'size': '${width ?? defaultWidth}x${height ?? defaultHeight}',
'zoom': zoomLevel.toString(),
'maptype': _getMapTypeQueryParam(mapType),
'key': googleMapsApiKey,
};
}
if (center != null)
finalUri.queryParameters['center'] =
'${center.latitude},${center.longitude}';
var uri = finalUri.build();
return uri;
}
String _getMapTypeQueryParam(StaticMapViewType maptype) {
String mapTypeQueryParam;
switch (maptype) {
case StaticMapViewType.roadmap:
mapTypeQueryParam = "roadmap";
break;
case StaticMapViewType.satellite:
mapTypeQueryParam = "satellite";
break;
case StaticMapViewType.hybrid:
mapTypeQueryParam = "hybrid";
break;
case StaticMapViewType.terrain:
mapTypeQueryParam = "terrain";
break;
}
return mapTypeQueryParam;
}
}