Created
August 19, 2015 16:05
-
-
Save wolever/0fd6a665ca94b6421d2e to your computer and use it in GitHub Desktop.
Google Maps with IPython Notebook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import json | |
| from jinja2 import Template | |
| from IPython.core.display import HTML, Javascript | |
| def init_maps(): | |
| HTML(""" | |
| <script>function dummy() {};</script> | |
| <script src="https://maps.googleapis.com/maps/api/js?libraries=visualization&callback=dummy"></script> | |
| """) | |
| def heatmap_points(points): | |
| pos = lambda x: {"lat": x[0], "lng": x[1]} | |
| center = points.mean(axis=0) | |
| html = Template(""" | |
| <div id="heatmap" style="height: 500px" class="map-canvas"></div> | |
| <script> | |
| (function() { | |
| var map = new google.maps.Map(document.getElementById('heatmap'), { | |
| zoom: 4, | |
| center: {{center}}, | |
| }); | |
| var heatmap = new google.maps.visualization.HeatmapLayer({ | |
| data: {{data}}.map(function(x) { return new google.maps.LatLng(x[0], x[1]); }), | |
| map: map | |
| }); | |
| heatmap.set('radius', 30); | |
| })(); | |
| </script> | |
| """).render( | |
| center=json.dumps(pos(center)), | |
| data=json.dumps(points.tolist()), | |
| ) | |
| return html |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment