From 3d184790fa88e0d333b7d40f286f934ab57bb7ac Mon Sep 17 00:00:00 2001 From: Mark Colley <39366189+M-Colley@users.noreply.github.com> Date: Wed, 2 Dec 2026 15:42:09 +0000 Subject: [PATCH] fix(maps): support list of dicts for static_map style --- googlemaps/maps.py | 4 +-- tests/test_maps.py | 67 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 2 deletions(-) diff --git a/googlemaps/maps.py b/googlemaps/maps.py index 746223d6..0e67962f 100644 --- a/googlemaps/maps.py +++ b/googlemaps/maps.py @@ -176,7 +176,7 @@ def static_map(client, size, :param style: defines a custom style to alter the presentation of a specific feature (roads, parks, and other features) of the map. - :type style: list of dict + :type style: dict or list of dict :rtype: iterator containing the raw image data, which typically can be used to save an image file locally. For example: @@ -236,7 +236,7 @@ def static_map(client, size, params["visible"] = convert.location_list(visible) if style: - params["style"] = convert.components(style) + params["style"] = [convert.components(s) for s in convert.as_list(style)] response = client._request( "/maps/api/staticmap", diff --git a/tests/test_maps.py b/tests/test_maps.py index 8db6298f..77470e68 100644 --- a/tests/test_maps.py +++ b/tests/test_maps.py @@ -127,3 +127,70 @@ def test_download(self): self.client.static_map( size=(400, 400), center=(63.259591, -144.667969), zoom=6, maptype="test" ) + + @responses.activate + def test_static_map_style_list(self): + url = "https://maps.googleapis.com/maps/api/staticmap" + responses.add(responses.GET, url, status=200) + + styles = [ + {"feature": "poi.business", "visibility": "off"}, + {"feature": "poi.schools", "visibility": "off"}, + {"feature": "poi.restaurants", "visibility": "on"}, + ] + + response = self.client.static_map( + size=(400, 400), + center=(63.259591, -144.667969), + zoom=6, + style=styles, + ) + + self.assertTrue(isinstance(response, GeneratorType)) + self.assertEqual(1, len(responses.calls)) + self.assertURLEqual( + "%s?center=63.259591%%2C-144.667969&size=400x400&" + "style=feature%%3Apoi.business%%7Cvisibility%%3Aoff&" + "style=feature%%3Apoi.schools%%7Cvisibility%%3Aoff&" + "style=feature%%3Apoi.restaurants%%7Cvisibility%%3Aon&" + "zoom=6&key=%s" % (url, self.key), + responses.calls[0].request.url, + ) + + @responses.activate + def test_static_map_style_dict(self): + url = "https://maps.googleapis.com/maps/api/staticmap" + responses.add(responses.GET, url, status=200) + + response = self.client.static_map( + size=(400, 400), + center=(63.259591, -144.667969), + zoom=6, + style={"feature": "poi.business", "visibility": "off"}, + ) + + self.assertTrue(isinstance(response, GeneratorType)) + self.assertEqual(1, len(responses.calls)) + self.assertURLEqual( + "%s?center=63.259591%%2C-144.667969&size=400x400&" + "style=feature%%3Apoi.business%%7Cvisibility%%3Aoff&" + "zoom=6&key=%s" % (url, self.key), + responses.calls[0].request.url, + ) + + def test_static_map_style_invalid(self): + with self.assertRaises(TypeError): + self.client.static_map( + size=(400, 400), + center=(63.259591, -144.667969), + zoom=6, + style="feature:poi.business|visibility:off", + ) + + with self.assertRaises(TypeError): + self.client.static_map( + size=(400, 400), + center=(63.259591, -144.667969), + zoom=6, + style=[{"feature": "poi.business"}, "invalid"], + )