In [1]:
from IPython.core.display import HTML, Javascript
In [2]:
# load the Google Maps API library
# TO DO:  make it easy to add API key

def gmap_init():
    js = """
window.gmap_initialize = function() {};
$.getScript('https://maps.googleapis.com/maps/api/js?v=3&sensor=false&callback=gmap_initialize');
"""
    return Javascript(data=js)

gmap_init()
Out[2]:
In [3]:
%%html
<style type="text/css">
  .map-canvas { height: 300px; }
</style
In [4]:
# generate a random id

import uuid
div_id = 'i' + str(uuid.uuid4())

html = """<div id="%s" class="map-canvas"/>""" % (div_id)

js = """
<script type="text/Javascript">
  (function(){
    var mapOptions = {
        zoom: 8,
        center: new google.maps.LatLng(-34.397, 150.644)
      };

    var map = new google.maps.Map(document.getElementById('%s'),
          mapOptions);
  })();  
</script>
""" % (div_id)

HTML(html+js)
Out[4]:
In [5]:
import uuid

def gmap(lat=37.8717,long=-122.2728,zoom=8):

    div_id = 'i' + str(uuid.uuid4())

    html = """<div id="%s" class="map-canvas"/>""" % (div_id)

    js = """
    <script type="text/Javascript">
      (function(){
        var mapOptions = {
            zoom: %s,
            center: new google.maps.LatLng(%s, %s)
          };

        var map = new google.maps.Map(document.getElementById('%s'),
              mapOptions);
      })();  
    </script>
    """ % (zoom, lat,long, div_id)

    return HTML(html+js)
In [6]:
import jinja2

TEMPLATE = """{{greeting}}, {{name}}"""

my_template = jinja2.Template(TEMPLATE)
my_template.render(greeting="hello", name="RY")
Out[6]:
u'hello, RY'

Can we ask google for list of instantiated maps?

Plotting markers

https://developers.google.com/maps/documentation/javascript/markers

var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
var mapOptions = {
  zoom: 4,
  center: myLatlng
}
var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);

// To add the marker to the map, use the 'map' property
var marker = new google.maps.Marker({
    position: myLatlng,
    map: map,
    title:"Hello World!"
});
In [7]:
%%html
<div id="markers" class="map-canvas"/>
In [8]:
%%javascript

var myLatlng = new google.maps.LatLng(37.8717,-122.2728);

var mapOptions = {
  zoom: 8,
  center: myLatlng
};

var map = new google.maps.Map(document.getElementById('markers'),
  mapOptions);

// To add the marker to the map, use the 'map' property
var marker = new google.maps.Marker({
    position: myLatlng,
    map: map,
    title:"Berkeley"
});

Drawing Circles on Map

https://developers.google.com/maps/documentation/javascript/examples/circle-simple

// This example creates circles on the map, representing
// populations in the United States.

// First, create an object containing LatLng and population for each city.
var citymap = {};
citymap['chicago'] = {
  center: new google.maps.LatLng(41.878113, -87.629798),
  population: 2842518
};
citymap['newyork'] = {
  center: new google.maps.LatLng(40.714352, -74.005973),
  population: 8143197
};
citymap['losangeles'] = {
  center: new google.maps.LatLng(34.052234, -118.243684),
  population: 3844829
};
var cityCircle;

function initialize() {
  // Create the map.
  var mapOptions = {
    zoom: 4,
    center: new google.maps.LatLng(37.09024, -95.712891),
    mapTypeId: google.maps.MapTypeId.TERRAIN
  };

  var map = new google.maps.Map(document.getElementById('map-canvas'),
      mapOptions);

  // Construct the circle for each value in citymap.
  // Note: We scale the population by a factor of 20.
  for (var city in citymap) {
    var populationOptions = {
      strokeColor: '#FF0000',
      strokeOpacity: 0.8,
      strokeWeight: 2,
      fillColor: '#FF0000',
      fillOpacity: 0.35,
      map: map,
      center: citymap[city].center,
      radius: citymap[city].population / 20
    };
    // Add the circle for this city to the map.
    cityCircle = new google.maps.Circle(populationOptions);
  }
}

google.maps.event.addDomListener(window, 'load', initialize);
In [9]:
%%html
<div id="circles" class="map-canvas"/>
In [10]:
%%javascript

// This example creates circles on the map, representing
// populations in the United States.

// First, create an object containing LatLng and population for each city.
var citymap = {};
citymap['chicago'] = {
  center: new google.maps.LatLng(41.878113, -87.629798),
  population: 2842518
};
citymap['newyork'] = {
  center: new google.maps.LatLng(40.714352, -74.005973),
  population: 8143197
};
citymap['losangeles'] = {
  center: new google.maps.LatLng(34.052234, -118.243684),
  population: 3844829
};
var cityCircle;


var mapOptions = {
    zoom: 4,
    center: new google.maps.LatLng(37.09024, -95.712891),
    mapTypeId: google.maps.MapTypeId.TERRAIN
};


  var map = new google.maps.Map(document.getElementById('circles'),
      mapOptions);

  // Construct the circle for each value in citymap.
  // Note: We scale the population by a factor of 20.
  for (var city in citymap) {
    var populationOptions = {
      strokeColor: '#FF0000',
      strokeOpacity: 0.8,
      strokeWeight: 2,
      fillColor: '#FF0000',
      fillOpacity: 0.35,
      map: map,
      center: citymap[city].center,
      radius: citymap[city].population / 20
    };
    // Add the circle for this city to the map.
    cityCircle = new google.maps.Circle(populationOptions);
  }
In [10]:
 

This website does not host notebooks, it only renders notebooks available on other websites.

Delivered by Fastly, Rendered by Rackspace

nbviewer GitHub repository.

nbviewer version: 4f5d1d8

nbconvert version: 5.1.1

Rendered (Sun, 23 Apr 2017 19:48:47 UTC)