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 import GoogleMap
from import GMarker, GEvent

def google_map(request):
    points = [
        {'lat':'35.42',     'lng': '139.42', 'href':''},
        {'lat':'51.30',     'lng':   '0.73', 'href':''},
        {'lat':'40.43',     'lng': '-74.0',  'href':''},
        {'lat':'34.03',     'lng':'-118.15', 'href':''},
        {'lat':'36.774402', 'lng':'-119.755405', 'href':''},]
    markers = []
    for point in points:
        marker = GMarker('POINT(%s %s)' % (point['lng'], point['lat']))
        event = GEvent('click', 'function() { location.href = "%s"}' % point['href'])
    google = GoogleMap(center=(0,0), zoom=1, markers=markers,
    return render_to_response('google_map.html',
                              {'google': google,})


Here's your google_maps.html template.:

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


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!