Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 49 additions & 1 deletion gmplot/gmplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ def __init__(self, center_lat, center_lng, zoom):
self.coloricon = os.path.join(os.path.dirname(__file__), 'markers/%s.png')
self.color_dict = mpl_color_map
self.html_color_codes = html_color_codes
self.title = 'Google Maps - gmplot'
self._fitBounds = None
self._symbols = {}

@classmethod
def from_geocode(cls, location_string, zoom=13):
Expand Down Expand Up @@ -109,6 +112,7 @@ def _process_kwargs(self, kwargs):
settings[key] = color

settings["closed"] = kwargs.get("closed", None)
settings["icons"] = kwargs.get("icons", {})

return settings

Expand Down Expand Up @@ -168,6 +172,23 @@ def polygon(self, lats, lngs, color=None, c=None, **kwargs):
shape = zip(lats, lngs)
self.shapes.append((shape, settings))

def fitBounds(self, latNE, lngNE, latSW, lngSW):
"""adjust the map zoom to fit the given bounds"""

# TODO: the bounds coords could be computed automatically, by updating them
# for every new object added to the map
self._fitBounds = (latNE, lngNE, latSW, lngSW)

def add_symbol(self, name, properties):
"""add a gmap symbol to the map objects, which can then be used in markers/polylines

name: the variable name (which you will reference in the other objects)
properties: a dictionary with the symbol properties

Ref: https://developers.google.com/maps/documentation/javascript/symbols"""

self._symbols[name] = properties

# create the html file which include one google map and all points and
# paths
def draw(self, htmlfile):
Expand All @@ -178,16 +199,18 @@ def draw(self, htmlfile):
'<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />\n')
f.write(
'<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>\n')
f.write('<title>Google Maps - pygmaps </title>\n')
f.write('<title>%s</title>\n' % self.title)
f.write('<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?libraries=visualization&sensor=true_or_false"></script>\n')
f.write('<script type="text/javascript">\n')
f.write('\tfunction initialize() {\n')
self.write_map(f)
self.write_symbols(f) # symbols have to be defined before being used
self.write_grids(f)
self.write_points(f)
self.write_paths(f)
self.write_shapes(f)
self.write_heatmap(f)
self.write_fitBounds(f)
f.write('\t}\n')
f.write('</script>\n')
f.write('</head>\n')
Expand Down Expand Up @@ -290,9 +313,17 @@ def write_point(self, f, lat, lon, color):
def write_polyline(self, f, path, settings):
clickable = False
geodesic = True
icons = ''
_icons = settings.get('icons', {})
strokeColor = settings.get('color') or settings.get('edge_color')
strokeOpacity = settings.get('edge_alpha')
strokeWeight = settings.get('edge_width')
for k in _icons:
# icon argument must not be in quotes
if k == 'icon':
icons += '%s: %s, ' % (k, _icons[k])
else:
icons += "%s: '%s', " % (k, _icons[k])

f.write('var PolylineCoordinates = [\n')
for coordinate in path:
Expand All @@ -305,6 +336,7 @@ def write_polyline(self, f, path, settings):
f.write('clickable: %s,\n' % (str(clickable).lower()))
f.write('geodesic: %s,\n' % (str(geodesic).lower()))
f.write('path: PolylineCoordinates,\n')
f.write('icons: [{%s}],\n' % icons)
f.write('strokeColor: "%s",\n' % (strokeColor))
f.write('strokeOpacity: %f,\n' % (strokeOpacity))
f.write('strokeWeight: %d\n' % (strokeWeight))
Expand Down Expand Up @@ -359,6 +391,22 @@ def write_heatmap(self, f):
f.write('heatmap.setMap(map);' + '\n')
f.write(settings_string)

def write_fitBounds(self, f):
if self._fitBounds:
f.write('\n')
f.write('rectBounds = new google.maps.LatLngBounds(\n')
f.write(' new google.maps.LatLng(%f, %f),\n' % self._fitBounds[:2])
f.write(' new google.maps.LatLng(%f, %f));\n' % self._fitBounds[2:])
f.write('map.fitBounds(rectBounds);\n')

def write_symbols(self, f):
for name in self._symbols:
f.write('\n')
f.write('var %s = {\n' % name)
for k in self._symbols[name]:
f.write(' %s: %s,\n' % (k, self._symbols[name][k]))
f.write('};\n\n')

if __name__ == "__main__":

mymap = GoogleMapPlotter(37.428, -122.145, 16)
Expand Down