Changeset 7213
- Timestamp:
- 03/10/08 15:45:51 (4 months ago)
- Files:
-
- django/branches/gis/django/contrib/gis/maps/google/gmap.py (modified) (3 diffs)
- django/branches/gis/django/contrib/gis/maps/google/__init__.py (modified) (3 diffs)
- django/branches/gis/django/contrib/gis/maps/google/overlays.py (added)
- django/branches/gis/django/contrib/gis/maps/google/zoom.py (modified) (3 diffs)
- django/branches/gis/django/contrib/gis/templates/gis/google/js/google-map.js (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/gis/django/contrib/gis/maps/google/gmap.py
r7027 r7213 1 1 from django.conf import settings 2 from django.contrib.gis import geos 2 3 from django.template.loader import render_to_string 4 from django.utils.safestring import mark_safe 5 6 # Declaring the GoogleMapException prior to getting the 7 # default `GZOOM` GoogleZoom instance. 8 class GoogleMapException(Exception): pass 9 from django.contrib.gis.maps.google.overlays import GPolygon, GPolyline 10 from django.contrib.gis.maps.google.zoom import GoogleZoom 11 GZOOM = GoogleZoom() 3 12 4 13 # The default Google Maps URL (for the API javascript) … … 6 15 GOOGLE_MAPS_URL='http://maps.google.com/maps?file=api&v=%s&key=' 7 16 8 class GoogleMapException(Exception): pass9 10 17 class GoogleMap(object): 11 "A class for generating Google Maps javascript."18 "A class for generating Google Maps JavaScript." 12 19 13 20 # String constants 14 onunload = 'onunload="GUnload()"'# Cleans up after Google Maps15 vml_css = 'v\:* {behavior:url(#default#VML);}'# CSS for IE VML16 xmlns = 'xmlns:v="urn:schemas-microsoft-com:vml"'# XML Namespace (for IE VML).21 onunload = mark_safe('onunload="GUnload()"') # Cleans up after Google Maps 22 vml_css = mark_safe('v\:* {behavior:url(#default#VML);}') # CSS for IE VML 23 xmlns = mark_safe('xmlns:v="urn:schemas-microsoft-com:vml"') # XML Namespace (for IE VML). 17 24 18 25 def __init__(self, key=None, api_url=None, version=None, 19 center_lat=0.0, center_lon=0.0, zoom=1, 20 dom_id='map', load_func='gmap_load', 21 kml_urls=[], template='gis/google/js/google-map.js', 26 center=None, center_lat=0.0, center_lon=0.0, 27 zoom=None, dom_id='map', load_func='gmap_load', 28 kml_urls=[], polygons=[], polylines=[], 29 template='gis/google/js/google-map.js', 22 30 extra_context={}): 23 31 … … 27 35 if not key: 28 36 try: 29 self. _key = settings.GOOGLE_MAPS_API_KEY37 self.key = settings.GOOGLE_MAPS_API_KEY 30 38 except AttributeError: 31 raise GoogleMapException , 'Google Maps API Key not found (try adding GOOGLE_MAPS_API_KEY to your settings).'39 raise GoogleMapException('Google Maps API Key not found (try adding GOOGLE_MAPS_API_KEY to your settings).') 32 40 else: 33 self. _key = key41 self.key = key 34 42 35 43 # Getting the Google Maps API version, defaults to using the latest ("2.x"), 36 44 # this is not necessarily the most stable. 37 45 if not version: 38 try: 39 self._version = settings.GOOGLE_MAPS_API_VERSION 40 except AttributeError: 41 self._version = '2.x' 46 self.version = getattr(settings, 'GOOGLE_MAPS_API_VERSION', '2.x') 42 47 else: 43 self. _version = version48 self.version = version 44 49 45 50 # Can specify the API URL in the `api_url` keyword. 46 51 if not api_url: 47 try: 48 self._url = settings.GOOGLE_MAPS_URL % self._version 49 except AttributeError: 50 self._url = GOOGLE_MAPS_URL % self._version 52 self.api_url = mark_safe(getattr(settings, 'GOOGLE_MAPS_URL', GOOGLE_MAPS_URL) % self.version) 51 53 else: 52 self. _url = api_url54 self.api_url = api_url 53 55 54 # Setting the DOM id of the map, the center lat/lon, the load function,55 # and the zoom.56 # Setting the DOM id of the map, the load function, the JavaScript 57 # template, and the KML URLs array. 56 58 self.dom_id = dom_id 57 self.center_lat = center_lat58 self.center_lon = center_lon59 59 self.load_func = load_func 60 60 self.template = template 61 self.zoom = zoom 61 self.kml_urls = kml_urls 62 63 # Does the user want any GPolygon or GPolyline overlays? 64 self.polygons, self.polylines = [], [] 65 if polygons: 66 for poly in polygons: 67 if isinstance(poly, GPolygon): 68 self.polygons.append(poly) 69 else: 70 self.polygons.append(GPolygon(poly)) 71 if polylines: 72 for pline in polylines: 73 if isinstance(pline, GPolyline): 74 self.polylines.append(pline) 75 else: 76 self.polylines.append(GPolyline(pline)) 77 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) 100 self.center = center 62 101 63 102 # Setting the parameters for the javascript template. 64 params = {'center_lat' : center_lat, 65 'center_lon' : center_lon, 66 'dom_id' : dom_id, 67 'kml_urls' : kml_urls, 68 'load_func' : load_func, 69 'zoom' : zoom, 103 params = {'center' : self.center, 104 'dom_id' : self.dom_id, 105 'kml_urls' : self.kml_urls, 106 'load_func' : self.load_func, 107 'zoom' : self.zoom, 108 'polygons' : self.polygons, 109 'polylines' : self.polylines, 70 110 } 71 111 params.update(extra_context) 72 self.js = render_to_string( template, params)112 self.js = render_to_string(self.template, params) 73 113 74 114 @property 75 115 def body(self): 76 116 "Returns HTML body tag for loading and unloading Google Maps javascript." 77 return '<body %s %s>' % (self.onload, self.onunload)117 return mark_safe('<body %s %s>' % (self.onload, self.onunload)) 78 118 79 119 @property 80 120 def onload(self): 81 121 "Returns the `onload` HTML <body> attribute." 82 return 'onload="%s()"' % self.load_func122 return mark_safe('onload="%s()"' % self.load_func) 83 123 84 124 @property 85 125 def api_script(self): 86 126 "Returns the <script> tag for the Google Maps API javascript." 87 return '<script src="%s%s" type="text/javascript"></script>' % (self._url, self._key)127 return mark_safe('<script src="%s%s" type="text/javascript"></script>' % (self.api_url, self.key)) 88 128 89 129 @property 90 130 def scripts(self): 91 131 "Returns all <script></script> tags required for Google Maps JavaScript." 92 return '%s\n <script type="text/javascript">\n//<![CDATA[\n%s//]]>\n </script>' % (self.api_script, self.js)132 return mark_safe('%s\n <script type="text/javascript">\n//<![CDATA[\n%s//]]>\n </script>' % (self.api_script, self.js)) 93 133 94 134 @property 95 135 def style(self): 96 136 "Returns additional CSS styling needed for Google Maps on IE." 97 return '<style type="text/css">%s</style>' % self.vml_css137 return mark_safe('<style type="text/css">%s</style>' % self.vml_css) 98 138 99 139 @property 100 140 def xhtml(self): 101 141 "Returns XHTML information needed for IE VML overlays." 102 return '<html xmlns="http://www.w3.org/1999/xhtml" %s>' % self.xmlns142 return mark_safe('<html xmlns="http://www.w3.org/1999/xhtml" %s>' % self.xmlns) django/branches/gis/django/contrib/gis/maps/google/__init__.py
r6108 r7213 20 20 </head> 21 21 {{ google.body }} 22 <div id="{{ google.dom_id }}" ></div>22 <div id="{{ google.dom_id }}" style="width:600px;height:400px;"></div> 23 23 </body> 24 24 </html> … … 48 48 <body onload="gmap_load()" onunload="GUnload()"> 49 49 50 - The ` id` property returns the DOM id for the map. Defaults to "map".50 - The `dom_id` property returns the DOM id for the map. Defaults to "map". 51 51 52 52 The following attributes may be set or customized in your local settings: … … 57 57 version. 58 58 """ 59 from django.contrib.gis.maps.google.gmap import GoogleMap 59 from django.contrib.gis.maps.google.gmap import GoogleMap, GZOOM 60 from django.contrib.gis.maps.google.overlays import GPolygon, GPolyline 60 61 from django.contrib.gis.maps.google.zoom import GoogleZoom django/branches/gis/django/contrib/gis/maps/google/zoom.py
r6108 r7213 100 100 "Converts a pixel to a longitude, latitude pair at the given zoom level." 101 101 if len(px) != 2: 102 raise TypeError , 'Pixel should be a sequence of two elements.'102 raise TypeError('Pixel should be a sequence of two elements.') 103 103 104 104 # Getting the number of pixels for the given zoom level. … … 139 139 140 140 # Checking the input type. 141 if not isinstance(geom, GEOSGeometry) and geom.srid == 4326:142 raise TypeError , 'get_zoom() expects a GEOS Geometry with an SRID of 4326.'141 if not isinstance(geom, GEOSGeometry) or geom.srid != 4326: 142 raise TypeError('get_zoom() expects a GEOS Geometry with an SRID of 4326.') 143 143 144 144 # Getting the envelope for the geometry, and its associated width, height … … 157 157 if (env_w > tile_w) or (env_h > tile_h): 158 158 if z == 0: 159 raise GoogleMapException, \ 160 'Geometry width and height should not exceed that of the Earth.' 159 raise GoogleMapException('Geometry width and height should not exceed that of the Earth.') 161 160 return z-1 162 161 163 162 # Otherwise, we've zoomed in to the max. 164 163 return self._nzoom-1 164 django/branches/gis/django/contrib/gis/templates/gis/google/js/google-map.js
r6108 r7213 1 {% block vars %}var map;{% for kml_url in kml_urls %}var kml{{ forloop.counter }};{% endfor %}{% endblock %}1 {% autoescape off %}{% block vars %}var map;{% endblock %} 2 2 {% block functions %}{% endblock %} 3 3 {% block load %}function {{ load_func }}(){ 4 4 if (GBrowserIsCompatible()) { 5 5 map = new GMap2(document.getElementById("{{ dom_id }}")); 6 map.addControl(new GSmallMapControl());7 map.addControl(new GMapTypeControl()); 8 map.setCenter(new GLatLng({{ center _lat }}, {{ center_lon}}), {{ zoom }});9 {% for kml_url in kml_urls %} kml{{ forloop.counter }} = new GGeoXml("{{ kml_url }}");6 {% block controls %}map.addControl(new GSmallMapControl()); 7 map.addControl(new GMapTypeControl());{% endblock %} 8 map.setCenter(new GLatLng({{ center.1 }}, {{ center.0 }}), {{ zoom }}); 9 {% for kml_url in kml_urls %}var kml{{ forloop.counter }} = new GGeoXml("{{ kml_url }}"); 10 10 map.addOverlay(kml{{ forloop.counter }});{% endfor %} 11 {% for polygon in polygons %}var poly{{ forloop.counter }} = new {{ polygon }}; 12 map.addOverlay(poly{{ forloop.counter }});{% endfor %} 13 {% for polyline in polylines %}var polyline{{ forloop.counter }} = new {{ polyline }}; 14 map.addOverlay(polyline{{ forloop.counter }});{% endfor %} 11 15 {% block load_extra %}{% endblock %} 12 16 }else { … … 14 18 } 15 19 } 16 {% endblock %} 20 {% endblock %}{% endautoescape %}
