1
0
mirror of https://github.com/django/django.git synced 2025-07-03 17:29: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
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

View File

@ -3,12 +3,8 @@ from django.contrib.gis import geos
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 @@ class GoogleMap(object):
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,

View File

@ -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.");