Permalink
Browse files

gis: `GoogleMap`: The Google Maps JavaScript API is now used for auto…

…matic 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
  • Loading branch information...
1 parent 17baee8 commit 4a636ec3a375107d490a72a4f05c5c16b69d9d8e @jbronn jbronn committed Apr 4, 2008
@@ -56,6 +56,6 @@
* GOOGLE_MAPS_URL (optional): Must have a substitution ('%s') for the API
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.zoom import GoogleZoom
@@ -3,12 +3,8 @@
from django.template.loader import render_to_string
from django.utils.safestring import mark_safe
-# Declaring the GoogleMapException prior to getting the
-# default `GZOOM` GoogleZoom instance.
class GoogleMapException(Exception): pass
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)
# TODO: Internationalize for Japan, UK, etc.
@@ -22,9 +18,8 @@ class GoogleMap(object):
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).
- def __init__(self, key=None, api_url=None, version=None,
- center=None, center_lat=0.0, center_lon=0.0,
- zoom=None, dom_id='map', load_func='gmap_load',
+ def __init__(self, key=None, api_url=None, version=None,
+ center=None, zoom=None, dom_id='map', load_func='gmap_load',
kml_urls=[], polygons=[], polylines=[],
template='gis/google/js/google-map.js',
extra_context={}):
@@ -75,32 +70,25 @@ def __init__(self, key=None, api_url=None, version=None,
else:
self.polylines.append(GPolyline(pline))
- # Automatically determining the zoom level if there are
- # GPolygon and/or GPolyline overlays.
- if (self.polygons or self.polylines) and zoom is None:
- envelopes = [p.envelope for p in self.polygons]
- envelopes.extend([p.envelope for p in self.polylines])
- # Creating a MultiPolygon of all the envelopes, this will
- # be used in determining the zoom level.
- zoom = geos.MultiPolygon(envelopes)
- zoom.srid = 4326
-
- # If a GEOSGeometry object is passed in for the `zoom` keyword
- # argument, then try to automatically determine an appropriate
- # zoom level.
- if isinstance(zoom, geos.GEOSGeometry):
- self.zoom = GZOOM.get_zoom(zoom)
- 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)
+ # If 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.
+ self.calc_zoom = False
+ if self.polygons or self.polylines:
+ if center is None or zoom is None:
+ self.calc_zoom = True
+
+ # Defaults for the zoom level and center coordinates if the zoom
+ # is not automatically calculated.
+ if zoom is None: zoom = 4
+ self.zoom = zoom
+ if center is None: center = (0, 0)
self.center = center
# 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,
'kml_urls' : self.kml_urls,
'load_func' : self.load_func,
@@ -5,13 +5,16 @@
map = new GMap2(document.getElementById("{{ dom_id }}"));
{% block controls %}map.addControl(new GSmallMapControl());
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 }}");
map.addOverlay(kml{{ forloop.counter }});{% endfor %}
{% 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 }};
- 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 %}
}else {
alert("Sorry, the Google Maps API is not compatible with this browser.");

0 comments on commit 4a636ec

Please sign in to comment.