Generating a Google Static Maps API with markers

Go

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']
          LABELS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
          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.")
                  
                    
          style_marker={}
          style_marker['size']=size
          style_marker['color']=color
          style_marker['label']=label
          style_marker['icon']=icon_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()] 
          routin.append(location)
          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 = 'https://maps.googleapis.com/maps/api/staticmap?'
          map_dict={}
          map_dict['scale']=scale
          size_str = str(size[0]) + 'x' + str(size[1])  # tuple of ints, up to 640 by 640
          map_dict['size']=size_str
          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
                    else:
                              markers_loc = [] 
                              for location in locations:
                                        location=str(location)
                                        location_list=location.split(',')
                                        if len(location_list) >2:
                                                  loc= marker_styler(location, marker_option)
                                        else:
                                                  loc = marker_styler(location, marker_option)
                                        markers_loc.append(loc)
                    #maptype: 'roadmap', 'satellite', 'hybrid', 'terrain
                    #markers
                    map_dict['markers']=markers_loc
          map_dict['maptype']=maptype
                    
          #map_dict['markers']
      
          query_str = urlencode(map_dict, doseq=True)
          url=base_url + query_str
          return url

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

Leave a Reply

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