google-maps-services-python icon indicating copy to clipboard operation
google-maps-services-python copied to clipboard

Street View Static API

Open tjwebb opened this issue 1 year ago • 1 comments

This library supports the static map API. It should also support the Static Street View API in the same way.

https://developers.google.com/maps/documentation/streetview/overview

tjwebb avatar Jul 24 '24 18:07 tjwebb

Hello @tjwebb,

I also needed the streetview API, so I make the method for it. Write this at the start of your code:

from googlemaps.client import make_api_method
import googlemaps

def streetview(client, location, size=None, **kwargs):
    """Get static image from Streetview API

    :param location: The latitude/longitude value representing the location to
        look up or a string
    :type location: string, dict

    :param size: The width/height value representing the image size to request
    :type size: list

    :param kwargs: Optional parameters available at https://developers.google.com/maps/documentation/streetview/request-streetview
    Warn: these parameter are not verified and you are responsible for the formatting

    :rtype: bytes
    """
    if type(location) not in (str, dict):
        raise ValueError(
            "Valid values for the `location` param for "
            "`streetview` are string or dict (lat/lng Object), "
            "the given value is invalid: '%s'" % input_type
        )
    
    if type(location) == dict and ("lat" not in location or "lng" not in location):
        raise ValueError(
            "Invalid value for the 'location' param "
            "the dict passed misses at least one of "
            "the following key 'lat', 'lng' "
            "the given value is invalid: '%s'" % location
        )
    
    if size:
        if type(size) != list or len(size) != 2:
            raise TypeError(
                "Invalid type for the 'size' param "
                "size should be of type list containing "
                "width and height as integers "
                "the given value is invalid: '%s'" % size
            )
        size = "{}x{}".format(*size)
    else:
        size = "600x400"

    params = {
        "location": "{lat},{lng}".format(**location),
        "size": size
    } | kwargs
    
    return client._request("/maps/api/streetview", params, extract_body=lambda x: x.content)

googlemaps.Client.streetview = make_api_method(streetview)

Example usage:

gmaps = googlemaps.Client(key="your-key")

test_streetview = gmaps.streetview(location={'lat': 0, 'lng': 0})

This output bytes object corresponding to the image data in jpeg, you can use Pillow or other lib of your choice to parse the image. If you need to use optional parameters, just add key: value to the function call corresponding to the documentation.

I provide this code with no guaranty nor support.

Ledge2494 avatar Apr 02 '25 12:04 Ledge2494