Basic Google Maps with Django

I'm in the process of upgrading a site from Django 1.0 to 1.3 and ran into an unexpected problem. There are a number of pages that use django.contrib.gis to display Google Maps, and these all inexplicably stopped working. These maps don't use any of Django's serious GIS features, they're just maps with a bunch of points on them.

After banging my head against my desk for awhile I figured out the problem and remembered how frustrating it was to get it set up in the first place--there don't seem to be any good tutorials on how to just display a simple map. Hopefully this will fill that void. <!-- SUMMARY_END -->

So here's a basic Django view in which we place a series of markers with links:

from django.conf import settings
from django.shortcuts import render_to_response
from django.contrib.gis.maps.google.gmap import GoogleMap
from django.contrib.gis.maps.google.overlays import GMarker, GEvent

def google_map(request):
    points = [
        {'lat':'35.42',     'lng': '139.42', 'href':'http://en.wikipedia.org/wiki/Tokyo'},
        {'lat':'51.30',     'lng':   '0.73', 'href':'http://en.wikipedia.org/wiki/London'},
        {'lat':'40.43',     'lng': '-74.0',  'href':'http://en.wikipedia.org/wiki/New_York_City'},
        {'lat':'34.03',     'lng':'-118.15', 'href':'http://en.wikipedia.org/wiki/Los_Angeles'},
        {'lat':'36.774402', 'lng':'-119.755405', 'href':'http://en.wikipedia.org/wiki/Fresno'},]
    markers = []
    for point in points:
        marker = GMarker('POINT(%s %s)' % (point['lng'], point['lat']))
        event = GEvent('click', 'function() { location.href = "%s"}' % point['href'])
        marker.add_event(event)
        markers.append(marker)
    google = GoogleMap(center=(0,0), zoom=1, markers=markers,
                       key=settings.GOOGLE_MAPS_API_PASSWORD)
    return render_to_response('google_map.html',
                              {'google': google,})

Notes:

Here's your google_maps.html template.:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
{{ google.xhtml }}
<head>
  {{ google.style }}
  {{ google.scripts }}
</head>
<body onload="{{ google.js_module }}.map_load()" onunload="GUnload()">
  <div id="{{ google.dom_id }}" style="width:600px; height:400px;"></div>
</body>
</html>

Notes:

And there you have it. You should see a nice Google Map with some points plotted on it. Thanks for visiting, and enjoy your map-making!