| 1 | from django.conf import settings
|
|---|
| 2 | from django.template.loader import render_to_string
|
|---|
| 3 |
|
|---|
| 4 | # The default Google Maps URL (for the API javascript)
|
|---|
| 5 | # TODO: Internationalize for Japan, UK, etc.
|
|---|
| 6 | GOOGLE_MAPS_URL='http://maps.google.com/maps?file=api&v=%s&key='
|
|---|
| 7 |
|
|---|
| 8 | class GoogleMapException(Exception): pass
|
|---|
| 9 |
|
|---|
| 10 | class GoogleMap(object):
|
|---|
| 11 | "A class for generating Google Maps javascript."
|
|---|
| 12 |
|
|---|
| 13 | # String constants
|
|---|
| 14 | onunload = 'onunload="GUnload()"' # Cleans up after Google Maps
|
|---|
| 15 | vml_css = 'v\:* {behavior:url(#default#VML);}' # CSS for IE VML
|
|---|
| 16 | xmlns = 'xmlns:v="urn:schemas-microsoft-com:vml"' # XML Namespace (for IE VML).
|
|---|
| 17 |
|
|---|
| 18 | def __init__(self, key=None, api_url=None, version=None,
|
|---|
| 19 | center_lat=0.0, center_lon=0.0, zoom=1,
|
|---|
| 20 | dom_id='map', load_func='gmap_load',
|
|---|
| 21 | kml_urls=[], template='gis/google/js/google-map.js',
|
|---|
| 22 | extra_context={}):
|
|---|
| 23 |
|
|---|
| 24 | # The Google Maps API Key defined in the settings will be used
|
|---|
| 25 | # if not passed in as a parameter. The use of an API key is
|
|---|
| 26 | # _required_.
|
|---|
| 27 | if not key:
|
|---|
| 28 | try:
|
|---|
| 29 | self._key = settings.GOOGLE_MAPS_API_KEY
|
|---|
| 30 | except AttributeError:
|
|---|
| 31 | raise GoogleMapException, 'Google Maps API Key not found (try adding GOOGLE_MAPS_API_KEY to your settings).'
|
|---|
| 32 | else:
|
|---|
| 33 | self._key = key
|
|---|
| 34 |
|
|---|
| 35 | # Getting the Google Maps API version, defaults to using the latest ("2.x"),
|
|---|
| 36 | # this is not necessarily the most stable.
|
|---|
| 37 | if not version:
|
|---|
| 38 | try:
|
|---|
| 39 | self._version = settings.GOOGLE_MAPS_API_VERSION
|
|---|
| 40 | except AttributeError:
|
|---|
| 41 | self._version = '2.x'
|
|---|
| 42 | else:
|
|---|
| 43 | self._version = version
|
|---|
| 44 |
|
|---|
| 45 | # Can specify the API URL in the `api_url` keyword.
|
|---|
| 46 | if not api_url:
|
|---|
| 47 | try:
|
|---|
| 48 | self._url = settings.GOOGLE_MAPS_URL % self._version
|
|---|
| 49 | except AttributeError:
|
|---|
| 50 | self._url = GOOGLE_MAPS_URL % self._version
|
|---|
| 51 | else:
|
|---|
| 52 | self._url = api_url
|
|---|
| 53 |
|
|---|
| 54 | # Setting the DOM id of the map, the center lat/lon, the load function,
|
|---|
| 55 | # and the zoom.
|
|---|
| 56 | self.dom_id = dom_id
|
|---|
| 57 | self.center_lat = center_lat
|
|---|
| 58 | self.center_lon = center_lon
|
|---|
| 59 | self.load_func = load_func
|
|---|
| 60 | self.template = template
|
|---|
| 61 | self.zoom = zoom
|
|---|
| 62 |
|
|---|
| 63 | # Setting the parameters for the javascript template.
|
|---|
| 64 | params = {'center_lat' : center_lat,
|
|---|
| 65 | 'center_lon' : center_lon,
|
|---|
| 66 | 'dom_id' : dom_id,
|
|---|
| 67 | 'kml_urls' : kml_urls,
|
|---|
| 68 | 'load_func' : load_func,
|
|---|
| 69 | 'zoom' : zoom,
|
|---|
| 70 | }
|
|---|
| 71 | params.update(extra_context)
|
|---|
| 72 | self.js = render_to_string(template, params)
|
|---|
| 73 |
|
|---|
| 74 | @property
|
|---|
| 75 | def body(self):
|
|---|
| 76 | "Returns HTML body tag for loading and unloading Google Maps javascript."
|
|---|
| 77 | return '<body %s %s>' % (self.onload, self.onunload)
|
|---|
| 78 |
|
|---|
| 79 | @property
|
|---|
| 80 | def onload(self):
|
|---|
| 81 | "Returns the `onload` HTML <body> attribute."
|
|---|
| 82 | return 'onload="%s()"' % self.load_func
|
|---|
| 83 |
|
|---|
| 84 | @property
|
|---|
| 85 | def api_script(self):
|
|---|
| 86 | "Returns the <script> tag for the Google Maps API javascript."
|
|---|
| 87 | return '<script src="%s%s" type="text/javascript"></script>' % (self._url, self._key)
|
|---|
| 88 |
|
|---|
| 89 | @property
|
|---|
| 90 | def scripts(self):
|
|---|
| 91 | "Returns all <script></script> tags required for Google Maps JavaScript."
|
|---|
| 92 | return '%s\n <script type="text/javascript">\n//<![CDATA[\n%s//]]>\n </script>' % (self.api_script, self.js)
|
|---|
| 93 |
|
|---|
| 94 | @property
|
|---|
| 95 | def style(self):
|
|---|
| 96 | "Returns additional CSS styling needed for Google Maps on IE."
|
|---|
| 97 | return '<style type="text/css">%s</style>' % self.vml_css
|
|---|
| 98 |
|
|---|
| 99 | @property
|
|---|
| 100 | def xhtml(self):
|
|---|
| 101 | "Returns XHTML information needed for IE VML overlays."
|
|---|
| 102 | return '<html xmlns="http://www.w3.org/1999/xhtml" %s>' % self.xmlns
|
|---|