Generating a Google Static Maps API with markers


The Google Static Maps API lets you embed a Google Maps image on your web page without requiring JavaScript or any dynamic page loading. The Google Static Maps API service creates your map based on URL parameters sent through a standard HTTP request and returns the map as an image you can display on your web page.

The Google Static Maps API returns an image (either GIF, PNG or JPEG) in response to an HTTP request via a URL. For each request, you can specify the location of the map, the size of the image, the zoom level, the type of map, and the placement of optional markers at locations on the map. You can additionally label your markers using alphanumeric characters.

In this post I decided to write a function to generating a Google Static Maps with marker as easy as possible here is my version:

from six.moves.urllib.parse import urlencode, quote, urlparse 
import googlemaps
import webbrowser
import urllib

# check icon url
def check_url(url):
          result = urlparse(url)
          return result.scheme and result.netloc  

# create marker style , icon_url
def marker_option(size, color, label, icon_url):
          SIZES = ['tiny', 'mid', 'small']
          COLORS = ['black', 'brown', 'green', 'purple', 'yellow', 'blue', 'gray', 'orange', 'red', 'white'] 
          if size and size not in SIZES:
                    raise ValueError("Not a valid marker size. Valid sizes are %s" % SIZES)
          if label and label not in LABELS:
                    raise ValueError("Not a valid label. Valid labels are a single character 'A'..'Z' or '0'..'9'")
          if color and color not in COLORS:
                    raise ValueError("Not a valid color. Valid colors are %s" % COLORS)    
          if icon_url and not check_url(icon_url):
                    raise ValueError("Not a valid url.")
          return style_marker
# create marker style include location
def marker_styler(location, marker_option):
          routin = ["%s:%s" % (k, v) for k, v in marker_option.items()] 
          return '|'.join(routin)

# create static map url with markers
# scale can be 1 or 2
def static_url(file_name, locations=None, center=None, scale=1, zoom=None, size=(500,400), imgformat= 'png', maptype='satellite',key=None):
          base_url = ''
          size_str = str(size[0]) + 'x' + str(size[1])  # tuple of ints, up to 640 by 640
          if center !=None:
                    map_dict['center']=str(center[0])+ ","+str(center[1])
          if zoom !=None:
                    map_dict['zoom']= zoom      
          if locations!= None:
                    if type(locations) is not list:
                              markers_loc = locations
                              markers_loc = [] 
                              for location in locations:
                                        if len(location_list) >2:
                                                  loc= marker_styler(location, marker_option)
                                                  loc = marker_styler(location, marker_option)
                    #maptype: 'roadmap', 'satellite', 'hybrid', 'terrain
          query_str = urlencode(map_dict, doseq=True)
          url=base_url + query_str
          return url

import webbrowser
# open url browser 
def url_browser(url):

Leave a Reply

Your email address will not be published. Required fields are marked *