1
0
mirror of https://github.com/django/django.git synced 2025-07-04 09:49:12 +00:00

gis: GoogleMap: The Google Maps JavaScript API is now used for automatic zoom level determination; the center_lat and center_lon keywords have been removed.

git-svn-id: http://code.djangoproject.com/svn/django/branches/gis@7400 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Justin Bronn 2008-04-04 12:57:19 +00:00
parent 17baee8789
commit 4a636ec3a3
3 changed files with 25 additions and 34 deletions

View File

@ -56,6 +56,6 @@
* GOOGLE_MAPS_URL (optional): Must have a substitution ('%s') for the API * GOOGLE_MAPS_URL (optional): Must have a substitution ('%s') for the API
version. version.
""" """
from django.contrib.gis.maps.google.gmap import GoogleMap, GZOOM from django.contrib.gis.maps.google.gmap import GoogleMap
from django.contrib.gis.maps.google.overlays import GPolygon, GPolyline from django.contrib.gis.maps.google.overlays import GPolygon, GPolyline
from django.contrib.gis.maps.google.zoom import GoogleZoom from django.contrib.gis.maps.google.zoom import GoogleZoom

View File

@ -3,12 +3,8 @@ from django.contrib.gis import geos
from django.template.loader import render_to_string from django.template.loader import render_to_string
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
# Declaring the GoogleMapException prior to getting the
# default `GZOOM` GoogleZoom instance.
class GoogleMapException(Exception): pass class GoogleMapException(Exception): pass
from django.contrib.gis.maps.google.overlays import GPolygon, GPolyline from django.contrib.gis.maps.google.overlays import GPolygon, GPolyline
from django.contrib.gis.maps.google.zoom import GoogleZoom
GZOOM = GoogleZoom()
# The default Google Maps URL (for the API javascript) # The default Google Maps URL (for the API javascript)
# TODO: Internationalize for Japan, UK, etc. # TODO: Internationalize for Japan, UK, etc.
@ -23,8 +19,7 @@ class GoogleMap(object):
xmlns = mark_safe('xmlns:v="urn:schemas-microsoft-com:vml"') # XML Namespace (for IE VML). xmlns = mark_safe('xmlns:v="urn:schemas-microsoft-com:vml"') # XML Namespace (for IE VML).
def __init__(self, key=None, api_url=None, version=None, def __init__(self, key=None, api_url=None, version=None,
center=None, center_lat=0.0, center_lon=0.0, center=None, zoom=None, dom_id='map', load_func='gmap_load',
zoom=None, dom_id='map', load_func='gmap_load',
kml_urls=[], polygons=[], polylines=[], kml_urls=[], polygons=[], polylines=[],
template='gis/google/js/google-map.js', template='gis/google/js/google-map.js',
extra_context={}): extra_context={}):
@ -75,32 +70,25 @@ class GoogleMap(object):
else: else:
self.polylines.append(GPolyline(pline)) self.polylines.append(GPolyline(pline))
# Automatically determining the zoom level if there are # If GPolygons and/or GPolylines are used the zoom will be automatically
# GPolygon and/or GPolyline overlays. # calculated via the Google Maps API. If both a zoom level and a
if (self.polygons or self.polylines) and zoom is None: # center coordinate are provided with polygons/polylines, no automatic
envelopes = [p.envelope for p in self.polygons] # determination will occur.
envelopes.extend([p.envelope for p in self.polylines]) self.calc_zoom = False
# Creating a MultiPolygon of all the envelopes, this will if self.polygons or self.polylines:
# be used in determining the zoom level. if center is None or zoom is None:
zoom = geos.MultiPolygon(envelopes) self.calc_zoom = True
zoom.srid = 4326
# If a GEOSGeometry object is passed in for the `zoom` keyword # Defaults for the zoom level and center coordinates if the zoom
# argument, then try to automatically determine an appropriate # is not automatically calculated.
# zoom level. if zoom is None: zoom = 4
if isinstance(zoom, geos.GEOSGeometry): self.zoom = zoom
self.zoom = GZOOM.get_zoom(zoom) if center is None: center = (0, 0)
else:
self.zoom = zoom
# The map center coordinate -- the `center_lon` and `center_lat` keyword
# are deprecated.
if not center:
center = (center_lon, center_lat)
self.center = center self.center = center
# Setting the parameters for the javascript template. # Setting the parameters for the javascript template.
params = {'center' : self.center, params = {'calc_zoom' : self.calc_zoom,
'center' : self.center,
'dom_id' : self.dom_id, 'dom_id' : self.dom_id,
'kml_urls' : self.kml_urls, 'kml_urls' : self.kml_urls,
'load_func' : self.load_func, 'load_func' : self.load_func,

View File

@ -5,13 +5,16 @@
map = new GMap2(document.getElementById("{{ dom_id }}")); map = new GMap2(document.getElementById("{{ dom_id }}"));
{% block controls %}map.addControl(new GSmallMapControl()); {% block controls %}map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());{% endblock %} map.addControl(new GMapTypeControl());{% endblock %}
map.setCenter(new GLatLng({{ center.1 }}, {{ center.0 }}), {{ zoom }}); {% if calc_zoom %}var bounds = new GLatLngBounds(); var tmp_bounds = new GLatLngBounds();{% else %}map.setCenter(new GLatLng({{ center.1 }}, {{ center.0 }}), {{ zoom }});{% endif %}
{% for kml_url in kml_urls %}var kml{{ forloop.counter }} = new GGeoXml("{{ kml_url }}"); {% for kml_url in kml_urls %}var kml{{ forloop.counter }} = new GGeoXml("{{ kml_url }}");
map.addOverlay(kml{{ forloop.counter }});{% endfor %} map.addOverlay(kml{{ forloop.counter }});{% endfor %}
{% for polygon in polygons %}var poly{{ forloop.counter }} = new {{ polygon }}; {% for polygon in polygons %}var poly{{ forloop.counter }} = new {{ polygon }};
map.addOverlay(poly{{ forloop.counter }});{% endfor %} map.addOverlay(poly{{ forloop.counter }});{% if calc_zoom %}
tmp_bounds = poly{{ forloop.counter }}.getBounds(); bounds.extend(tmp_bounds.getSouthWest()); bounds.extend(tmp_bounds.getNorthEast());{% endif %}{% endfor %}
{% for polyline in polylines %}var polyline{{ forloop.counter }} = new {{ polyline }}; {% for polyline in polylines %}var polyline{{ forloop.counter }} = new {{ polyline }};
map.addOverlay(polyline{{ forloop.counter }});{% endfor %} map.addOverlay(polyline{{ forloop.counter }});{% if calc_zoom %}
tmp_bounds = polyline{{ forloop.counter }}.getBounds(); bounds.extend(tmp_bounds.getSouthWest()); bounds.extend(tmp_bounds.getNorthEast());{% endif %}{% endfor %}
{% if calc_zoom %}map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));{% endif %}
{% block load_extra %}{% endblock %} {% block load_extra %}{% endblock %}
}else { }else {
alert("Sorry, the Google Maps API is not compatible with this browser."); alert("Sorry, the Google Maps API is not compatible with this browser.");