Skip to content
This repository has been archived by the owner on Jun 25, 2019. It is now read-only.

Dev branch #135

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
210 changes: 188 additions & 22 deletions lib/static_map_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ class StaticMapProvider {
///

Uri getStaticUri(Location center, int zoomLevel,
{int width, int height, StaticMapViewType mapType}) {
{int width, int height, StaticMapViewType mapType, String style}) {
return _buildUrl(
null,
null,
center,
zoomLevel ?? defaultZoomLevel,
width ?? defaultWidth,
height ?? defaultHeight,
mapType ?? defaultMaptype);
mapType ?? defaultMaptype, false, style ?? "");
}

///
Expand All @@ -39,9 +40,34 @@ class StaticMapProvider {
///

Uri getStaticUriWithMarkers(List<Marker> markers,
{int width, int height, StaticMapViewType maptype, Location center}) {
return _buildUrl(markers, center, null, width ?? defaultWidth,
height ?? defaultHeight, maptype ?? defaultMaptype);
{int width, int height, StaticMapViewType maptype, Location center, bool customIcon, String style}) {
return _buildUrl(null, markers, center, null, width ?? defaultWidth,
height ?? defaultHeight, maptype ?? defaultMaptype, customIcon ?? false, style ?? "");
}

///
/// Creates a Uri for the Google Static Maps API using a list of locations to create a path on the map
/// [locations] must have at least 2 locations
/// Specify a [width] and [height] that you would like the resulting image to be. The default is 600w x 400h
///

Uri getStaticUriWithPath(List<Location> points,
{int width, int height, StaticMapViewType maptype, Location center, String style}) {
return _buildUrl(points, null, center, null, width ?? defaultWidth,
height ?? defaultHeight, maptype ?? defaultMaptype, false, style ?? "");
}

///
/// Creates a Uri for the Google Static Maps API using a list of locations to create a path on the map and
/// uses a list of locations to create pins on the map
/// [locations] must have at least 2 locations
/// Specify a [width] and [height] that you would like the resulting image to be. The default is 600w x 400h
///

Uri getStaticUriWithPathAndMarkers(List<Location> points,List<Marker> markers,
{int width, int height, StaticMapViewType maptype, Location center, bool customIcon, String style}) {
return _buildUrl(points, markers, center, null, width ?? defaultWidth,
height ?? defaultHeight, maptype ?? defaultMaptype, customIcon ?? false, style ?? "");
}

///
Expand All @@ -55,9 +81,9 @@ class StaticMapProvider {
int height,
StaticMapViewType maptype,
Location center,
int zoomLevel}) {
return _buildUrl(markers, center, zoomLevel, width ?? defaultWidth,
height ?? defaultHeight, maptype ?? defaultMaptype);
int zoomLevel, bool customIcon, String style}) {
return _buildUrl(null, markers, center, zoomLevel, width ?? defaultWidth,
height ?? defaultHeight, maptype ?? defaultMaptype, customIcon ?? false, style ?? "");
}

///
Expand All @@ -67,56 +93,196 @@ class StaticMapProvider {
/// 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 {
{int width, int height, StaticMapViewType maptype, String style}) 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);
return _buildUrl(null, markers, center, zoom.toInt(), width ?? defaultWidth,
height ?? defaultHeight, maptype ?? defaultMaptype, false, style ?? "");
}

Uri _buildUrl(List<Marker> locations, Location center, int zoomLevel,
int width, int height, StaticMapViewType mapType) {
Uri _buildUrl(List<Location> points, List<Marker> locations, Location center, int zoomLevel,
int width, int height, StaticMapViewType mapType, bool customIcon, String style) {
var finalUri = new UriBuilder()
..scheme = 'https'
..host = 'maps.googleapis.com'
..port = 443
..path = '/maps/api/staticmap';

if (center == null && (locations == null || locations.length == 0)) {
var uri;

if (center == null && (locations == null || locations.length == 0)
&& (points == null || points.length < 2)) {
center = Locations.centerOfUSA;
}

if (locations == null || locations.length == 0) {
if (center == null) center = Locations.centerOfUSA;
if ((points != null && points.length >= 2) && (locations == null || locations.length == 0)) {
List<String> locs = new List();
points.forEach((location) {
num lat = location.latitude;
num lng = location.longitude;
String point = '$lat,$lng';
locs.add(point);
});
String pointsString = locs.join('|');
finalUri.queryParameters = {
'center': '${center.latitude},${center.longitude}',
'zoom': zoomLevel.toString(),
'path': pointsString,
'size': '${width ?? defaultWidth}x${height ?? defaultHeight}',
'maptype': _getMapTypeQueryParam(mapType),
'key': googleMapsApiKey,
};
} else {

if (style != "") {
finalUri.queryParameters['style'] = style;
}

}else if ((points != null && points.length >= 2) && (locations != null && locations.length > 0)) {
List<String> locs = new List();
points.forEach((location) {
num lat = location.latitude;
num lng = location.longitude;
String point = '$lat,$lng';
locs.add(point);
});
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('|');
String pointsString = locs.join('|');
if(customIcon) {
String size = '${width ?? defaultWidth}x${height ?? defaultHeight}';
uri = _createCustomMarkersUri(pointsString, locations, size, style);
} else {
String markersString = markers.join('|');
finalUri.queryParameters = {
'path': pointsString,
'markers': markersString,
'size': '${width ?? defaultWidth}x${height ?? defaultHeight}',
'maptype': _getMapTypeQueryParam(mapType),
'key': googleMapsApiKey,
};

if (style != "") {
finalUri.queryParameters['style'] = style;
}
}
}else if (locations == null || locations.length == 0) {
if (center == null) center = Locations.centerOfUSA;
finalUri.queryParameters = {
'markers': markersString,
'center': '${center.latitude},${center.longitude}',
'zoom': zoomLevel.toString(),
'size': '${width ?? defaultWidth}x${height ?? defaultHeight}',
'maptype': _getMapTypeQueryParam(mapType),
'key': googleMapsApiKey,
};

if (style != "") {
finalUri.queryParameters['style'] = style;
}
} else {
List<String> markers = new List();
locations.forEach((location) {
num lat = location.latitude;
num lng = location.longitude;
String marker = '$lat,$lng';
markers.add(marker);
});
if (customIcon){
String size = '${width ?? defaultWidth}x${height ?? defaultHeight}';
uri = _createCustomMarkersUri(null, locations, size, style);
} else {
String markersString = markers.join('|');
finalUri.queryParameters = {
'markers': markersString,
'size': '${width ?? defaultWidth}x${height ?? defaultHeight}',
'maptype': _getMapTypeQueryParam(mapType),
'key': googleMapsApiKey,
};

if (style != "") {
finalUri.queryParameters['style'] = style;
}
}
}
if (center != null)
finalUri.queryParameters['center'] =
'${center.latitude},${center.longitude}';

var uri = finalUri.build();
if (!customIcon) {
uri = finalUri.build();
}
return uri;
}

///
/// Creates a custom URI that allows the use of custom marker icons.
/// If there is a path, it should already be formatted correctly.
/// Locations contain the Custom Marker Icon.
/// Size is already formatted correctly.
///
Uri _createCustomMarkersUri(String path, List<Marker> locations, String size, String style) {
Uri uri;

List<String> icons = new List();
List<String> markers = new List();

locations.forEach((location) {

num lat = location.latitude;
num lng = location.longitude;
String marker = '$lat,$lng';
markers.add(marker);

String iconUrl = "";
String markerUrl = "";
bool isAsset = false;

try {
iconUrl = location.markerIcon.asset;
isAsset = true;
} catch (exception) {
isAsset = false;
}

if (isAsset) {
String iconUrl = location.markerIcon.asset;
markerUrl = ('&markers=icon:$iconUrl%7C$marker');
} else {
markerUrl = ('&markers=$marker');
}

icons.add(markerUrl);
});


String markersString = icons.join('%7C');

if (style == "") {
if (path != null) {
uri = Uri.parse(
'https://maps.googleapis.com/maps/api/staticmap?&size=$size&path=$path' +
markersString + '&key=$googleMapsApiKey');
} else {
uri = Uri.parse(
'https://maps.googleapis.com/maps/api/staticmap?&size=$size' +
markersString + '&key=$googleMapsApiKey');
}
} else {
if (path != null) {
uri = Uri.parse(
'https://maps.googleapis.com/maps/api/staticmap?&size=$size&style=$style&path=$path' +
markersString + '&key=$googleMapsApiKey');
} else {
uri = Uri.parse(
'https://maps.googleapis.com/maps/api/staticmap?&size=$size&style=$style' +
markersString + '&key=$googleMapsApiKey');
}
}


return uri;
}

Expand Down