From d8404864e49bde0ab6b205263deb95de97cd6887 Mon Sep 17 00:00:00 2001 From: Sam Carson Date: Tue, 25 Sep 2018 19:21:05 -0400 Subject: [PATCH 1/3] Added support for paths to the static_map_provider --- lib/static_map_provider.dart | 41 ++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/lib/static_map_provider.dart b/lib/static_map_provider.dart index 477e7a1..a256eb1 100644 --- a/lib/static_map_provider.dart +++ b/lib/static_map_provider.dart @@ -24,6 +24,7 @@ class StaticMapProvider { Uri getStaticUri(Location center, int zoomLevel, {int width, int height, StaticMapViewType mapType}) { return _buildUrl( + null, null, center, zoomLevel ?? defaultZoomLevel, @@ -40,7 +41,19 @@ class StaticMapProvider { Uri getStaticUriWithMarkers(List markers, {int width, int height, StaticMapViewType maptype, Location center}) { - return _buildUrl(markers, center, null, width ?? defaultWidth, + return _buildUrl(null, 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 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 points, + {int width, int height, StaticMapViewType maptype, Location center}) { + return _buildUrl(points, null, center, null, width ?? defaultWidth, height ?? defaultHeight, maptype ?? defaultMaptype); } @@ -56,7 +69,7 @@ class StaticMapProvider { StaticMapViewType maptype, Location center, int zoomLevel}) { - return _buildUrl(markers, center, zoomLevel, width ?? defaultWidth, + return _buildUrl(null, markers, center, zoomLevel, width ?? defaultWidth, height ?? defaultHeight, maptype ?? defaultMaptype); } @@ -71,11 +84,11 @@ class StaticMapProvider { var markers = await mapView.visibleAnnotations; var center = await mapView.centerLocation; var zoom = await mapView.zoomLevel; - return _buildUrl(markers, center, zoom.toInt(), width ?? defaultWidth, + return _buildUrl(null, markers, center, zoom.toInt(), width ?? defaultWidth, height ?? defaultHeight, maptype ?? defaultMaptype); } - Uri _buildUrl(List locations, Location center, int zoomLevel, + Uri _buildUrl(List points, List locations, Location center, int zoomLevel, int width, int height, StaticMapViewType mapType) { var finalUri = new UriBuilder() ..scheme = 'https' @@ -83,11 +96,27 @@ class StaticMapProvider { ..port = 443 ..path = '/maps/api/staticmap'; - if (center == null && (locations == null || locations.length == 0)) { + if (center == null && (locations == null || locations.length == 0) + && (points == null || points.length < 2)) { center = Locations.centerOfUSA; } - if (locations == null || locations.length == 0) { + if (points != null && points.length >= 2) { + List 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 = { + 'path': pointsString, + 'size': '${width ?? defaultWidth}x${height ?? defaultHeight}', + 'maptype': _getMapTypeQueryParam(mapType), + 'key': googleMapsApiKey, + }; + }else if (locations == null || locations.length == 0) { if (center == null) center = Locations.centerOfUSA; finalUri.queryParameters = { 'center': '${center.latitude},${center.longitude}', From c5b36a341b65613c5fa018f3bf8631b97b646eae Mon Sep 17 00:00:00 2001 From: Sam Carson Date: Mon, 22 Oct 2018 14:08:28 -0400 Subject: [PATCH 2/3] Added the ability to use custom icons for markers on the static map. This change will work for each function that places markers on a static map only when customIcon is set to true. Methods: getStaticUriWithMarkers getStaticUriWithMarkersAndZoom getStaticUriWithPathAndMarkers --- lib/static_map_provider.dart | 140 ++++++++++++++++++++++++++++++----- 1 file changed, 123 insertions(+), 17 deletions(-) diff --git a/lib/static_map_provider.dart b/lib/static_map_provider.dart index a256eb1..e356aef 100644 --- a/lib/static_map_provider.dart +++ b/lib/static_map_provider.dart @@ -30,7 +30,7 @@ class StaticMapProvider { zoomLevel ?? defaultZoomLevel, width ?? defaultWidth, height ?? defaultHeight, - mapType ?? defaultMaptype); + mapType ?? defaultMaptype, false); } /// @@ -40,9 +40,9 @@ class StaticMapProvider { /// Uri getStaticUriWithMarkers(List markers, - {int width, int height, StaticMapViewType maptype, Location center}) { + {int width, int height, StaticMapViewType maptype, Location center, bool customIcon}) { return _buildUrl(null, markers, center, null, width ?? defaultWidth, - height ?? defaultHeight, maptype ?? defaultMaptype); + height ?? defaultHeight, maptype ?? defaultMaptype, customIcon ?? false); } /// @@ -54,7 +54,20 @@ class StaticMapProvider { Uri getStaticUriWithPath(List points, {int width, int height, StaticMapViewType maptype, Location center}) { return _buildUrl(points, null, center, null, width ?? defaultWidth, - height ?? defaultHeight, maptype ?? defaultMaptype); + height ?? defaultHeight, maptype ?? defaultMaptype, false); + } + + /// + /// 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 points,List markers, + {int width, int height, StaticMapViewType maptype, Location center, bool customIcon}) { + return _buildUrl(points, markers, center, null, width ?? defaultWidth, + height ?? defaultHeight, maptype ?? defaultMaptype, customIcon ?? false); } /// @@ -68,9 +81,9 @@ class StaticMapProvider { int height, StaticMapViewType maptype, Location center, - int zoomLevel}) { + int zoomLevel, bool customIcon}) { return _buildUrl(null, markers, center, zoomLevel, width ?? defaultWidth, - height ?? defaultHeight, maptype ?? defaultMaptype); + height ?? defaultHeight, maptype ?? defaultMaptype, customIcon ?? false); } /// @@ -85,23 +98,25 @@ class StaticMapProvider { var center = await mapView.centerLocation; var zoom = await mapView.zoomLevel; return _buildUrl(null, markers, center, zoom.toInt(), width ?? defaultWidth, - height ?? defaultHeight, maptype ?? defaultMaptype); + height ?? defaultHeight, maptype ?? defaultMaptype, false); } Uri _buildUrl(List points, List locations, Location center, int zoomLevel, - int width, int height, StaticMapViewType mapType) { + int width, int height, StaticMapViewType mapType, bool customIcon) { var finalUri = new UriBuilder() ..scheme = 'https' ..host = 'maps.googleapis.com' ..port = 443 ..path = '/maps/api/staticmap'; + var uri; + if (center == null && (locations == null || locations.length == 0) && (points == null || points.length < 2)) { center = Locations.centerOfUSA; } - if (points != null && points.length >= 2) { + if ((points != null && points.length >= 2) && (locations == null || locations.length == 0)) { List locs = new List(); points.forEach((location) { num lat = location.latitude; @@ -116,6 +131,35 @@ class StaticMapProvider { 'maptype': _getMapTypeQueryParam(mapType), 'key': googleMapsApiKey, }; + }else if ((points != null && points.length >= 2) && (locations != null && locations.length > 0)) { + List locs = new List(); + points.forEach((location) { + num lat = location.latitude; + num lng = location.longitude; + String point = '$lat,$lng'; + locs.add(point); + }); + List markers = new List(); + locations.forEach((location) { + num lat = location.latitude; + num lng = location.longitude; + String marker = '$lat,$lng'; + markers.add(marker); + }); + String pointsString = locs.join('|'); + if(customIcon) { + String size = '${width ?? defaultWidth}x${height ?? defaultHeight}'; + uri = _createCustomMarkersUri(pointsString, locations, size); + } else { + String markersString = markers.join('|'); + finalUri.queryParameters = { + 'path': pointsString, + 'markers': markersString, + 'size': '${width ?? defaultWidth}x${height ?? defaultHeight}', + 'maptype': _getMapTypeQueryParam(mapType), + 'key': googleMapsApiKey, + }; + } }else if (locations == null || locations.length == 0) { if (center == null) center = Locations.centerOfUSA; finalUri.queryParameters = { @@ -133,19 +177,81 @@ class StaticMapProvider { String marker = '$lat,$lng'; markers.add(marker); }); - String markersString = markers.join('|'); - finalUri.queryParameters = { - 'markers': markersString, - 'size': '${width ?? defaultWidth}x${height ?? defaultHeight}', - 'maptype': _getMapTypeQueryParam(mapType), - 'key': googleMapsApiKey, - }; + if (customIcon){ + String size = '${width ?? defaultWidth}x${height ?? defaultHeight}'; + uri = _createCustomMarkersUri(null, locations, size); + } else { + String markersString = markers.join('|'); + finalUri.queryParameters = { + 'markers': markersString, + 'size': '${width ?? defaultWidth}x${height ?? defaultHeight}', + 'maptype': _getMapTypeQueryParam(mapType), + 'key': googleMapsApiKey, + }; + } } 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 locations, String size) { + Uri uri; + + List icons = new List(); + List 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 (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'); + } + return uri; } From 6b21b78695c93b7f6e8683d746b16dd4350530b2 Mon Sep 17 00:00:00 2001 From: Sam Carson Date: Wed, 24 Oct 2018 19:31:06 -0400 Subject: [PATCH 3/3] Added the ability to style the static maps --- lib/static_map_provider.dart | 77 +++++++++++++++++++++++++----------- 1 file changed, 54 insertions(+), 23 deletions(-) diff --git a/lib/static_map_provider.dart b/lib/static_map_provider.dart index e356aef..8862d1a 100644 --- a/lib/static_map_provider.dart +++ b/lib/static_map_provider.dart @@ -22,7 +22,7 @@ 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, @@ -30,7 +30,7 @@ class StaticMapProvider { zoomLevel ?? defaultZoomLevel, width ?? defaultWidth, height ?? defaultHeight, - mapType ?? defaultMaptype, false); + mapType ?? defaultMaptype, false, style ?? ""); } /// @@ -40,9 +40,9 @@ class StaticMapProvider { /// Uri getStaticUriWithMarkers(List markers, - {int width, int height, StaticMapViewType maptype, Location center, bool customIcon}) { + {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); + height ?? defaultHeight, maptype ?? defaultMaptype, customIcon ?? false, style ?? ""); } /// @@ -52,9 +52,9 @@ class StaticMapProvider { /// Uri getStaticUriWithPath(List points, - {int width, int height, StaticMapViewType maptype, Location center}) { + {int width, int height, StaticMapViewType maptype, Location center, String style}) { return _buildUrl(points, null, center, null, width ?? defaultWidth, - height ?? defaultHeight, maptype ?? defaultMaptype, false); + height ?? defaultHeight, maptype ?? defaultMaptype, false, style ?? ""); } /// @@ -65,9 +65,9 @@ class StaticMapProvider { /// Uri getStaticUriWithPathAndMarkers(List points,List markers, - {int width, int height, StaticMapViewType maptype, Location center, bool customIcon}) { + {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); + height ?? defaultHeight, maptype ?? defaultMaptype, customIcon ?? false, style ?? ""); } /// @@ -81,9 +81,9 @@ class StaticMapProvider { int height, StaticMapViewType maptype, Location center, - int zoomLevel, bool customIcon}) { + int zoomLevel, bool customIcon, String style}) { return _buildUrl(null, markers, center, zoomLevel, width ?? defaultWidth, - height ?? defaultHeight, maptype ?? defaultMaptype, customIcon ?? false); + height ?? defaultHeight, maptype ?? defaultMaptype, customIcon ?? false, style ?? ""); } /// @@ -93,16 +93,16 @@ class StaticMapProvider { /// Specify a [width] and [height] that you would like the resulting image to be. The default is 600w x 400h /// Future 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(null, markers, center, zoom.toInt(), width ?? defaultWidth, - height ?? defaultHeight, maptype ?? defaultMaptype, false); + height ?? defaultHeight, maptype ?? defaultMaptype, false, style ?? ""); } Uri _buildUrl(List points, List locations, Location center, int zoomLevel, - int width, int height, StaticMapViewType mapType, bool customIcon) { + int width, int height, StaticMapViewType mapType, bool customIcon, String style) { var finalUri = new UriBuilder() ..scheme = 'https' ..host = 'maps.googleapis.com' @@ -131,6 +131,11 @@ class StaticMapProvider { 'maptype': _getMapTypeQueryParam(mapType), 'key': googleMapsApiKey, }; + + if (style != "") { + finalUri.queryParameters['style'] = style; + } + }else if ((points != null && points.length >= 2) && (locations != null && locations.length > 0)) { List locs = new List(); points.forEach((location) { @@ -149,7 +154,7 @@ class StaticMapProvider { String pointsString = locs.join('|'); if(customIcon) { String size = '${width ?? defaultWidth}x${height ?? defaultHeight}'; - uri = _createCustomMarkersUri(pointsString, locations, size); + uri = _createCustomMarkersUri(pointsString, locations, size, style); } else { String markersString = markers.join('|'); finalUri.queryParameters = { @@ -159,6 +164,10 @@ class StaticMapProvider { 'maptype': _getMapTypeQueryParam(mapType), 'key': googleMapsApiKey, }; + + if (style != "") { + finalUri.queryParameters['style'] = style; + } } }else if (locations == null || locations.length == 0) { if (center == null) center = Locations.centerOfUSA; @@ -169,6 +178,10 @@ class StaticMapProvider { 'maptype': _getMapTypeQueryParam(mapType), 'key': googleMapsApiKey, }; + + if (style != "") { + finalUri.queryParameters['style'] = style; + } } else { List markers = new List(); locations.forEach((location) { @@ -179,7 +192,7 @@ class StaticMapProvider { }); if (customIcon){ String size = '${width ?? defaultWidth}x${height ?? defaultHeight}'; - uri = _createCustomMarkersUri(null, locations, size); + uri = _createCustomMarkersUri(null, locations, size, style); } else { String markersString = markers.join('|'); finalUri.queryParameters = { @@ -188,6 +201,10 @@ class StaticMapProvider { 'maptype': _getMapTypeQueryParam(mapType), 'key': googleMapsApiKey, }; + + if (style != "") { + finalUri.queryParameters['style'] = style; + } } } if (center != null) @@ -206,7 +223,7 @@ class StaticMapProvider { /// Locations contain the Custom Marker Icon. /// Size is already formatted correctly. /// - Uri _createCustomMarkersUri(String path, List locations, String size) { + Uri _createCustomMarkersUri(String path, List locations, String size, String style) { Uri uri; List icons = new List(); @@ -240,18 +257,32 @@ class StaticMapProvider { icons.add(markerUrl); }); + String markersString = icons.join('%7C'); - if (path != null) { - uri = Uri.parse( - 'https://maps.googleapis.com/maps/api/staticmap?&size=$size&path=$path' + - markersString + '&key=$googleMapsApiKey'); + 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 { - uri = Uri.parse( - 'https://maps.googleapis.com/maps/api/staticmap?&size=$size' + - markersString + '&key=$googleMapsApiKey'); + 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; }