Changeset 7400
- Timestamp:
- 04/04/08 07:57:19 (3 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/gis/django/contrib/gis/maps/google/gmap.py
r7213 r7400 4 4 from django.utils.safestring import mark_safe 5 5 6 # Declaring the GoogleMapException prior to getting the7 # default `GZOOM` GoogleZoom instance.8 6 class GoogleMapException(Exception): pass 9 7 from django.contrib.gis.maps.google.overlays import GPolygon, GPolyline 10 from django.contrib.gis.maps.google.zoom import GoogleZoom11 GZOOM = GoogleZoom()12 8 13 9 # The default Google Maps URL (for the API javascript) … … 23 19 xmlns = mark_safe('xmlns:v="urn:schemas-microsoft-com:vml"') # XML Namespace (for IE VML). 24 20 25 def __init__(self, key=None, api_url=None, version=None, 26 center=None, center_lat=0.0, center_lon=0.0, 27 zoom=None, dom_id='map', load_func='gmap_load', 21 def __init__(self, key=None, api_url=None, version=None, 22 center=None, zoom=None, dom_id='map', load_func='gmap_load', 28 23 kml_urls=[], polygons=[], polylines=[], 29 24 template='gis/google/js/google-map.js', … … 76 71 self.polylines.append(GPolyline(pline)) 77 72 78 # Automatically determining the zoom level if there are 79 # GPolygon and/or GPolyline overlays. 80 if (self.polygons or self.polylines) and zoom is None: 81 envelopes = [p.envelope for p in self.polygons] 82 envelopes.extend([p.envelope for p in self.polylines]) 83 # Creating a MultiPolygon of all the envelopes, this will 84 # be used in determining the zoom level. 85 zoom = geos.MultiPolygon(envelopes) 86 zoom.srid = 4326 87 88 # If a GEOSGeometry object is passed in for the `zoom` keyword 89 # argument, then try to automatically determine an appropriate 90 # zoom level. 91 if isinstance(zoom, geos.GEOSGeometry): 92 self.zoom = GZOOM.get_zoom(zoom) 93 else: 94 self.zoom = zoom 95 96 # The map center coordinate -- the `center_lon` and `center_lat` keyword 97 # are deprecated. 98 if not center: 99 center = (center_lon, center_lat) 73 # If GPolygons and/or GPolylines are used the zoom will be automatically 74 # calculated via the Google Maps API. If both a zoom level and a 75 # center coordinate are provided with polygons/polylines, no automatic 76 # determination will occur. 77 self.calc_zoom = False 78 if self.polygons or self.polylines: 79 if center is None or zoom is None: 80 self.calc_zoom = True 81 82 # Defaults for the zoom level and center coordinates if the zoom 83 # is not automatically calculated. 84 if zoom is None: zoom = 4 85 self.zoom = zoom 86 if center is None: center = (0, 0) 100 87 self.center = center 101 88 102 89 # Setting the parameters for the javascript template. 103 params = {'center' : self.center, 90 params = {'calc_zoom' : self.calc_zoom, 91 'center' : self.center, 104 92 'dom_id' : self.dom_id, 105 93 'kml_urls' : self.kml_urls, django/branches/gis/django/contrib/gis/maps/google/__init__.py
r7213 r7400 57 57 version. 58 58 """ 59 from django.contrib.gis.maps.google.gmap import GoogleMap , GZOOM59 from django.contrib.gis.maps.google.gmap import GoogleMap 60 60 from django.contrib.gis.maps.google.overlays import GPolygon, GPolyline 61 61 from django.contrib.gis.maps.google.zoom import GoogleZoom django/branches/gis/django/contrib/gis/templates/gis/google/js/google-map.js
r7213 r7400 6 6 {% block controls %}map.addControl(new GSmallMapControl()); 7 7 map.addControl(new GMapTypeControl());{% endblock %} 8 map.setCenter(new GLatLng({{ center.1 }}, {{ center.0 }}), {{ zoom }});8 {% if calc_zoom %}var bounds = new GLatLngBounds(); var tmp_bounds = new GLatLngBounds();{% else %}map.setCenter(new GLatLng({{ center.1 }}, {{ center.0 }}), {{ zoom }});{% endif %} 9 9 {% for kml_url in kml_urls %}var kml{{ forloop.counter }} = new GGeoXml("{{ kml_url }}"); 10 10 map.addOverlay(kml{{ forloop.counter }});{% endfor %} 11 11 {% for polygon in polygons %}var poly{{ forloop.counter }} = new {{ polygon }}; 12 map.addOverlay(poly{{ forloop.counter }});{% endfor %} 12 map.addOverlay(poly{{ forloop.counter }});{% if calc_zoom %} 13 tmp_bounds = poly{{ forloop.counter }}.getBounds(); bounds.extend(tmp_bounds.getSouthWest()); bounds.extend(tmp_bounds.getNorthEast());{% endif %}{% endfor %} 13 14 {% for polyline in polylines %}var polyline{{ forloop.counter }} = new {{ polyline }}; 14 map.addOverlay(polyline{{ forloop.counter }});{% endfor %} 15 map.addOverlay(polyline{{ forloop.counter }});{% if calc_zoom %} 16 tmp_bounds = polyline{{ forloop.counter }}.getBounds(); bounds.extend(tmp_bounds.getSouthWest()); bounds.extend(tmp_bounds.getNorthEast());{% endif %}{% endfor %} 17 {% if calc_zoom %}map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));{% endif %} 15 18 {% block load_extra %}{% endblock %} 16 19 }else {
