diff --git a/lib/static_map_provider.dart b/lib/static_map_provider.dart index 477e7a1..65f86e3 100644 --- a/lib/static_map_provider.dart +++ b/lib/static_map_provider.dart @@ -22,14 +22,15 @@ class StaticMapProvider { /// Uri getStaticUri(Location center, int zoomLevel, - {int width, int height, StaticMapViewType mapType}) { + {int width, int height, StaticMapViewType mapType, List style}) { return _buildUrl( + null, null, center, zoomLevel ?? defaultZoomLevel, width ?? defaultWidth, height ?? defaultHeight, - mapType ?? defaultMaptype); + mapType ?? defaultMaptype, false, style ?? [], null); } /// @@ -39,9 +40,34 @@ class StaticMapProvider { /// Uri getStaticUriWithMarkers(List 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, List style}) { + return _buildUrl(null, markers, center, null, width ?? defaultWidth, + height ?? defaultHeight, maptype ?? defaultMaptype, customIcon ?? false, style ?? [], null); + } + + /// + /// 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, List style, String pathColor}) { + return _buildUrl(points, null, center, null, width ?? defaultWidth, + height ?? defaultHeight, maptype ?? defaultMaptype, false, style ?? [], pathColor ?? null); + } + + /// + /// 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, List style, String pathColor}) { + return _buildUrl(points, markers, center, null, width ?? defaultWidth, + height ?? defaultHeight, maptype ?? defaultMaptype, customIcon ?? false, style ?? [], pathColor ?? null); } /// @@ -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, List style}) { + return _buildUrl(null, markers, center, zoomLevel, width ?? defaultWidth, + height ?? defaultHeight, maptype ?? defaultMaptype, customIcon ?? false, style ?? [], null); } /// @@ -67,35 +93,120 @@ 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, List 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 ?? [], null); } - Uri _buildUrl(List locations, Location center, int zoomLevel, - int width, int height, StaticMapViewType mapType) { + Uri _buildUrl(List points, List locations, Location center, int zoomLevel, + int width, int height, StaticMapViewType mapType, bool customIcon, List styles, String pathColor) { 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 ((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); + }); + String pointsString = locs.join('|'); + if(customIcon || styles.length > 1) { + String size = '${width ?? defaultWidth}x${height ?? defaultHeight}'; + uri = _createCustomMarkersUri(customIcon, pointsString, locations, size, styles, center, zoomLevel, pathColor); + } else { + + String pathColorStr = ""; + + if (pathColor != null) { + pathColorStr = "color:" + pathColor + "|"; + } + + finalUri.queryParameters = { + 'path': pathColorStr + pointsString, + 'size': '${width ?? defaultWidth}x${height ?? defaultHeight}', + 'maptype': _getMapTypeQueryParam(mapType), + 'key': googleMapsApiKey, + }; + + if (styles.length == 1) { + finalUri.queryParameters['style'] = styles[0]; + } + } + + }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 || styles.length > 1) { + String size = '${width ?? defaultWidth}x${height ?? defaultHeight}'; + uri = _createCustomMarkersUri(customIcon, pointsString, locations, size, styles, center, zoomLevel, pathColor); + } else { + + String pathColorStr = ""; + + if (pathColor != null) { + pathColorStr = "color:" + pathColor + "|"; + } + + String markersString = markers.join('|'); + finalUri.queryParameters = { + 'path': pathColorStr + pointsString, + 'markers': markersString, + 'size': '${width ?? defaultWidth}x${height ?? defaultHeight}', + 'maptype': _getMapTypeQueryParam(mapType), + 'key': googleMapsApiKey, + }; + + if (styles.length == 1) { + + finalUri.queryParameters['style'] = styles[0]; + } + } + }else 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, - }; + if (styles.length > 1){ + String size = '${width ?? defaultWidth}x${height ?? defaultHeight}'; + uri = _createCustomMarkersUri(customIcon, null, locations, size, styles, center, zoomLevel, pathColor); + } else { + finalUri.queryParameters = { + 'center': '${center.latitude},${center.longitude}', + 'zoom': zoomLevel.toString(), + 'size': '${width ?? defaultWidth}x${height ?? defaultHeight}', + 'maptype': _getMapTypeQueryParam(mapType), + 'key': googleMapsApiKey, + }; + + if (styles.length == 1) { + finalUri.queryParameters['style'] = styles[0]; + } + } } else { List markers = new List(); locations.forEach((location) { @@ -104,19 +215,129 @@ 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 || styles.length > 1){ + String size = '${width ?? defaultWidth}x${height ?? defaultHeight}'; + uri = _createCustomMarkersUri(customIcon, null, locations, size, styles, center, zoomLevel, pathColor); + } else { + String markersString = markers.join('|'); + finalUri.queryParameters = { + 'markers': markersString, + 'size': '${width ?? defaultWidth}x${height ?? defaultHeight}', + 'maptype': _getMapTypeQueryParam(mapType), + 'key': googleMapsApiKey, + }; + + if (styles.length == 1) { + + finalUri.queryParameters['style'] = styles[0]; + } + } } if (center != null) finalUri.queryParameters['center'] = '${center.latitude},${center.longitude}'; - var uri = finalUri.build(); + if (uri == null) { + 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(bool customIcon , String path, List locations, String size, List styles, + Location center, int zoom, String pathColor) { + + Uri uri; + + List icons = new List(); + List markers = new List(); + + if (locations != null) { + 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; + + if (customIcon) { + 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 finalStyle = ""; + + for (String s in styles) { + + finalStyle = finalStyle + '&style=' + s; + + } + + String centerString = ""; + String zoomLevel = ""; + String pathColorStr = ""; + + if (center != null) { + centerString = center.latitude.toString() + "," + + center.longitude.toString(); + } + + if (zoom != null) { + zoomLevel = zoom.toString(); + } + + if (pathColor != null) { + pathColorStr = "color:" + pathColor + "|"; + } + + String markersString = icons.join('%7C'); + + if (styles == null) { + if (path != null) { + uri = Uri.parse( + 'https://maps.googleapis.com/maps/api/staticmap?¢er=$centerString&zoom=$zoomLevel&size=$size&path=$pathColorStr$path' + + markersString + '&key=$googleMapsApiKey'); + } else { + uri = Uri.parse( + 'https://maps.googleapis.com/maps/api/staticmap?¢er=$centerString&zoom=$zoomLevel&size=$size' + + markersString + '&key=$googleMapsApiKey'); + } + } else { + if (path != null) { + uri = Uri.parse( + 'https://maps.googleapis.com/maps/api/staticmap?¢er=$centerString&zoom=$zoomLevel&size=$size$finalStyle&path=$pathColorStr$path' + + markersString + '&key=$googleMapsApiKey'); + } else { + uri = Uri.parse( + 'https://maps.googleapis.com/maps/api/staticmap?¢er=$centerString&zoom=$zoomLevel&size=$size$finalStyle' + + markersString + '&key=$googleMapsApiKey'); + } + } + return uri; }