2014-02-03 18:46:17 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
from django.conf import settings
|
|
|
|
from django.template.loader import render_to_string
|
2012-07-03 07:31:14 +08:00
|
|
|
from django.utils.html import format_html
|
2008-08-06 02:13:06 +08:00
|
|
|
from django.utils.safestring import mark_safe
|
2014-12-13 21:04:36 +08:00
|
|
|
from django.utils.six.moves import range
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2011-07-13 17:35:51 +08:00
|
|
|
from django.contrib.gis.maps.google.overlays import GPolygon, GPolyline, GMarker
|
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2011-07-13 17:35:51 +08:00
|
|
|
class GoogleMapException(Exception):
|
|
|
|
pass
|
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
# The default Google Maps URL (for the API javascript)
|
|
|
|
# TODO: Internationalize for Japan, UK, etc.
|
2013-10-23 18:09:29 +08:00
|
|
|
GOOGLE_MAPS_URL = 'http://maps.google.com/maps?file=api&v=%s&key='
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2011-07-13 17:35:51 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
class GoogleMap(object):
|
|
|
|
"A class for generating Google Maps JavaScript."
|
|
|
|
|
|
|
|
# String constants
|
2013-11-03 05:02:56 +08:00
|
|
|
onunload = mark_safe('onunload="GUnload()"') # Cleans up after Google Maps
|
|
|
|
vml_css = mark_safe('v\:* {behavior:url(#default#VML);}') # CSS for IE VML
|
|
|
|
xmlns = mark_safe('xmlns:v="urn:schemas-microsoft-com:vml"') # XML Namespace (for IE VML).
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2009-03-11 02:03:35 +08:00
|
|
|
def __init__(self, key=None, api_url=None, version=None,
|
2009-03-10 03:22:24 +08:00
|
|
|
center=None, zoom=None, dom_id='map',
|
2009-03-11 02:03:35 +08:00
|
|
|
kml_urls=[], polylines=None, polygons=None, markers=None,
|
2009-06-30 01:17:45 +08:00
|
|
|
template='gis/google/google-map.js',
|
2009-03-11 02:03:35 +08:00
|
|
|
js_module='geodjango',
|
|
|
|
extra_context={}):
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
# The Google Maps API Key defined in the settings will be used
|
2009-03-10 03:22:24 +08:00
|
|
|
# if not passed in as a parameter. The use of an API key is
|
|
|
|
# _required_.
|
2008-08-06 02:13:06 +08:00
|
|
|
if not key:
|
|
|
|
try:
|
|
|
|
self.key = settings.GOOGLE_MAPS_API_KEY
|
|
|
|
except AttributeError:
|
2014-09-04 20:15:09 +08:00
|
|
|
raise GoogleMapException(
|
|
|
|
'Google Maps API Key not found (try adding '
|
|
|
|
'GOOGLE_MAPS_API_KEY to your settings).'
|
|
|
|
)
|
2008-08-06 02:13:06 +08:00
|
|
|
else:
|
|
|
|
self.key = key
|
2009-03-11 02:03:35 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
# Getting the Google Maps API version, defaults to using the latest ("2.x"),
|
2009-03-10 03:22:24 +08:00
|
|
|
# this is not necessarily the most stable.
|
2008-08-06 02:13:06 +08:00
|
|
|
if not version:
|
|
|
|
self.version = getattr(settings, 'GOOGLE_MAPS_API_VERSION', '2.x')
|
|
|
|
else:
|
|
|
|
self.version = version
|
|
|
|
|
|
|
|
# Can specify the API URL in the `api_url` keyword.
|
|
|
|
if not api_url:
|
2012-07-03 07:33:44 +08:00
|
|
|
self.api_url = getattr(settings, 'GOOGLE_MAPS_URL', GOOGLE_MAPS_URL) % self.version
|
2008-08-06 02:13:06 +08:00
|
|
|
else:
|
|
|
|
self.api_url = api_url
|
|
|
|
|
|
|
|
# Setting the DOM id of the map, the load function, the JavaScript
|
|
|
|
# template, and the KML URLs array.
|
|
|
|
self.dom_id = dom_id
|
2009-03-10 03:22:24 +08:00
|
|
|
self.extra_context = extra_context
|
|
|
|
self.js_module = js_module
|
2008-08-06 02:13:06 +08:00
|
|
|
self.template = template
|
|
|
|
self.kml_urls = kml_urls
|
2009-03-11 02:03:35 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
# Does the user want any GMarker, GPolygon, and/or GPolyline overlays?
|
2009-03-11 02:03:35 +08:00
|
|
|
overlay_info = [[GMarker, markers, 'markers'],
|
|
|
|
[GPolygon, polygons, 'polygons'],
|
|
|
|
[GPolyline, polylines, 'polylines']]
|
|
|
|
|
|
|
|
for overlay_class, overlay_list, varname in overlay_info:
|
|
|
|
setattr(self, varname, [])
|
|
|
|
if overlay_list:
|
|
|
|
for overlay in overlay_list:
|
|
|
|
if isinstance(overlay, overlay_class):
|
|
|
|
getattr(self, varname).append(overlay)
|
|
|
|
else:
|
|
|
|
getattr(self, varname).append(overlay_class(overlay))
|
|
|
|
|
2009-03-10 03:22:24 +08:00
|
|
|
# If GMarker, GPolygons, and/or GPolylines are used the zoom will be
|
|
|
|
# automatically calculated via the Google Maps API. If both a zoom
|
|
|
|
# level and a center coordinate are provided with polygons/polylines,
|
|
|
|
# no automatic determination will occur.
|
2008-08-06 02:13:06 +08:00
|
|
|
self.calc_zoom = False
|
2013-10-11 19:25:14 +08:00
|
|
|
if self.polygons or self.polylines or self.markers:
|
2008-08-06 02:13:06 +08:00
|
|
|
if center is None or zoom is None:
|
|
|
|
self.calc_zoom = True
|
2009-03-11 02:03:35 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
# Defaults for the zoom level and center coordinates if the zoom
|
|
|
|
# is not automatically calculated.
|
2013-10-17 16:17:41 +08:00
|
|
|
if zoom is None:
|
|
|
|
zoom = 4
|
2008-08-06 02:13:06 +08:00
|
|
|
self.zoom = zoom
|
2013-10-17 16:17:41 +08:00
|
|
|
if center is None:
|
|
|
|
center = (0, 0)
|
2008-08-06 02:13:06 +08:00
|
|
|
self.center = center
|
|
|
|
|
2009-03-10 03:22:24 +08:00
|
|
|
def render(self):
|
|
|
|
"""
|
|
|
|
Generates the JavaScript necessary for displaying this Google Map.
|
|
|
|
"""
|
2013-10-27 09:27:42 +08:00
|
|
|
params = {'calc_zoom': self.calc_zoom,
|
|
|
|
'center': self.center,
|
|
|
|
'dom_id': self.dom_id,
|
|
|
|
'js_module': self.js_module,
|
|
|
|
'kml_urls': self.kml_urls,
|
|
|
|
'zoom': self.zoom,
|
|
|
|
'polygons': self.polygons,
|
|
|
|
'polylines': self.polylines,
|
2009-03-11 02:03:35 +08:00
|
|
|
'icons': self.icons,
|
2013-10-27 09:27:42 +08:00
|
|
|
'markers': self.markers,
|
2008-08-06 02:13:06 +08:00
|
|
|
}
|
2009-03-10 03:22:24 +08:00
|
|
|
params.update(self.extra_context)
|
|
|
|
return render_to_string(self.template, params)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def body(self):
|
|
|
|
"Returns HTML body tag for loading and unloading Google Maps javascript."
|
2014-11-27 08:41:27 +08:00
|
|
|
return format_html('<body {} {}>', self.onload, self.onunload)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def onload(self):
|
|
|
|
"Returns the `onload` HTML <body> attribute."
|
2014-11-27 08:41:27 +08:00
|
|
|
return format_html('onload="{}.{}_load()"', self.js_module, self.dom_id)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def api_script(self):
|
|
|
|
"Returns the <script> tag for the Google Maps API javascript."
|
2014-11-27 08:41:27 +08:00
|
|
|
return format_html('<script src="{}{}" type="text/javascript"></script>',
|
2012-07-03 07:31:14 +08:00
|
|
|
self.api_url, self.key)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2009-03-10 03:22:24 +08:00
|
|
|
@property
|
|
|
|
def js(self):
|
|
|
|
"Returns only the generated Google Maps JavaScript (no <script> tags)."
|
|
|
|
return self.render()
|
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
@property
|
|
|
|
def scripts(self):
|
2009-03-10 03:22:24 +08:00
|
|
|
"Returns all <script></script> tags required with Google Maps JavaScript."
|
2014-11-27 08:41:27 +08:00
|
|
|
return format_html('{}\n <script type="text/javascript">\n//<![CDATA[\n{}//]]>\n </script>',
|
2013-07-22 15:55:45 +08:00
|
|
|
self.api_script, mark_safe(self.js))
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def style(self):
|
|
|
|
"Returns additional CSS styling needed for Google Maps on IE."
|
2014-11-27 08:41:27 +08:00
|
|
|
return format_html('<style type="text/css">{}</style>', self.vml_css)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def xhtml(self):
|
|
|
|
"Returns XHTML information needed for IE VML overlays."
|
2014-11-27 08:41:27 +08:00
|
|
|
return format_html('<html xmlns="http://www.w3.org/1999/xhtml" {}>', self.xmlns)
|
2009-03-10 03:22:24 +08:00
|
|
|
|
2009-03-20 10:10:47 +08:00
|
|
|
@property
|
|
|
|
def icons(self):
|
|
|
|
"Returns a sequence of GIcon objects in this map."
|
2013-08-30 07:20:00 +08:00
|
|
|
return set(marker.icon for marker in self.markers if marker.icon)
|
2009-03-20 10:10:47 +08:00
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2009-03-10 03:22:24 +08:00
|
|
|
class GoogleMapSet(GoogleMap):
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
A class for generating sets of Google Maps that will be shown on the
|
|
|
|
same page together.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
gmapset = GoogleMapSet( GoogleMap( ... ), GoogleMap( ... ) )
|
|
|
|
gmapset = GoogleMapSet( [ gmap1, gmap2] )
|
|
|
|
"""
|
|
|
|
# The `google-multi.js` template is used instead of `google-single.js`
|
|
|
|
# by default.
|
|
|
|
template = kwargs.pop('template', 'gis/google/google-multi.js')
|
|
|
|
|
|
|
|
# This is the template used to generate the GMap load JavaScript for
|
|
|
|
# each map in the set.
|
2009-06-30 01:17:45 +08:00
|
|
|
self.map_template = kwargs.pop('map_template', 'gis/google/google-single.js')
|
2009-03-10 03:22:24 +08:00
|
|
|
|
|
|
|
# Running GoogleMap.__init__(), and resetting the template
|
|
|
|
# value with default obtained above.
|
|
|
|
super(GoogleMapSet, self).__init__(**kwargs)
|
|
|
|
self.template = template
|
|
|
|
|
|
|
|
# If a tuple/list passed in as first element of args, then assume
|
|
|
|
if isinstance(args[0], (tuple, list)):
|
|
|
|
self.maps = args[0]
|
|
|
|
else:
|
|
|
|
self.maps = args
|
2009-03-11 02:03:35 +08:00
|
|
|
|
2009-03-10 03:22:24 +08:00
|
|
|
# Generating DOM ids for each of the maps in the set.
|
2014-12-13 21:04:36 +08:00
|
|
|
self.dom_ids = ['map%d' % i for i in range(len(self.maps))]
|
2009-03-10 03:22:24 +08:00
|
|
|
|
|
|
|
def load_map_js(self):
|
|
|
|
"""
|
|
|
|
Returns JavaScript containing all of the loading routines for each
|
|
|
|
map in this set.
|
|
|
|
"""
|
|
|
|
result = []
|
|
|
|
for dom_id, gmap in zip(self.dom_ids, self.maps):
|
|
|
|
# Backup copies the GoogleMap DOM id and template attributes.
|
|
|
|
# They are overridden on each GoogleMap instance in the set so
|
|
|
|
# that only the loading JavaScript (and not the header variables)
|
|
|
|
# is used with the generated DOM ids.
|
|
|
|
tmp = (gmap.template, gmap.dom_id)
|
|
|
|
gmap.template = self.map_template
|
|
|
|
gmap.dom_id = dom_id
|
|
|
|
result.append(gmap.js)
|
|
|
|
# Restoring the backup values.
|
|
|
|
gmap.template, gmap.dom_id = tmp
|
|
|
|
return mark_safe(''.join(result))
|
|
|
|
|
|
|
|
def render(self):
|
|
|
|
"""
|
|
|
|
Generates the JavaScript for the collection of Google Maps in
|
|
|
|
this set.
|
|
|
|
"""
|
2013-10-27 09:27:42 +08:00
|
|
|
params = {'js_module': self.js_module,
|
|
|
|
'dom_ids': self.dom_ids,
|
|
|
|
'load_map_js': self.load_map_js(),
|
|
|
|
'icons': self.icons,
|
2009-03-10 03:22:24 +08:00
|
|
|
}
|
|
|
|
params.update(self.extra_context)
|
|
|
|
return render_to_string(self.template, params)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def onload(self):
|
|
|
|
"Returns the `onload` HTML <body> attribute."
|
|
|
|
# Overloaded to use the `load` function defined in the
|
|
|
|
# `google-multi.js`, which calls the load routines for
|
|
|
|
# each one of the individual maps in the set.
|
|
|
|
return mark_safe('onload="%s.load()"' % self.js_module)
|
2009-03-20 10:10:47 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def icons(self):
|
|
|
|
"Returns a sequence of all icons in each map of the set."
|
2009-05-31 00:10:23 +08:00
|
|
|
icons = set()
|
2013-10-17 16:17:41 +08:00
|
|
|
for map in self.maps:
|
|
|
|
icons |= map.icons
|
2009-03-20 10:10:47 +08:00
|
|
|
return icons
|