Ticket #14284: 14284_patch.diff
File 14284_patch.diff, 20.2 KB (added by , 14 years ago) |
---|
-
django/contrib/gis/templates/gis/google/google-map.js
1 1 {% load l10n %} 2 2 {% autoescape off %} 3 3 {% localize off %} 4 {% block vars %}var geodjango = {};{% for icon in icons %} 5 var {{ icon.varname }} = new GIcon(G_DEFAULT_ICON); 6 {% if icon.image %}{{ icon.varname }}.image = "{{ icon.image }}";{% endif %} 7 {% if icon.shadow %}{{ icon.varname }}.shadow = "{{ icon.shadow }}";{% endif %} {% if icon.shadowsize %}{{ icon.varname }}.shadowSize = new GSize({{ icon.shadowsize.0 }}, {{ icon.shadowsize.1 }});{% endif %} 8 {% if icon.iconanchor %}{{ icon.varname }}.iconAnchor = new GPoint({{ icon.iconanchor.0 }}, {{ icon.iconanchor.1 }});{% endif %} {% if icon.iconsize %}{{ icon.varname }}.iconSize = new GSize({{ icon.iconsize.0 }}, {{ icon.iconsize.1 }});{% endif %} 9 {% if icon.infowindowanchor %}{{ icon.varname }}.infoWindowAnchor = new GPoint({{ icon.infowindowanchor.0 }}, {{ icon.infowindowanchor.1 }});{% endif %}{% endfor %} 4 {% block vars %}var geodjango = {}; 10 5 {% endblock vars %}{% block functions %} 11 6 {% block load %}{{ js_module }}.{{ dom_id }}_load = function(){ 12 if (GBrowserIsCompatible()) { 13 {{ js_module }}.{{ dom_id }} = new GMap2(document.getElementById("{{ dom_id }}")); 14 {{ js_module }}.{{ dom_id }}.setCenter(new GLatLng({{ center.1 }}, {{ center.0 }}), {{ zoom }}); 15 {% block controls %}{{ js_module }}.{{ dom_id }}.setUIToDefault();{% endblock %} 16 {% if calc_zoom %}var bounds = new GLatLngBounds(); var tmp_bounds = new GLatLngBounds();{% endif %} 17 {% for kml_url in kml_urls %}{{ js_module }}.{{ dom_id }}_kml{{ forloop.counter }} = new GGeoXml("{{ kml_url }}"); 18 {{ js_module }}.{{ dom_id }}.addOverlay({{ js_module }}.{{ dom_id }}_kml{{ forloop.counter }});{% endfor %} 19 {% for polygon in polygons %}{{ js_module }}.{{ dom_id }}_poly{{ forloop.counter }} = new {{ polygon }}; 20 {{ js_module }}.{{ dom_id }}.addOverlay({{ js_module }}.{{ dom_id }}_poly{{ forloop.counter }}); 21 {% for event in polygon.events %}GEvent.addListener({{ js_module }}.{{ dom_id }}_poly{{ forloop.parentloop.counter }}, {{ event }});{% endfor %} 22 {% if calc_zoom %}tmp_bounds = {{ js_module }}.{{ dom_id }}_poly{{ forloop.counter }}.getBounds(); bounds.extend(tmp_bounds.getSouthWest()); bounds.extend(tmp_bounds.getNorthEast());{% endif %}{% endfor %} 23 {% for polyline in polylines %}{{ js_module }}.{{ dom_id }}_polyline{{ forloop.counter }} = new {{ polyline }}; 24 {{ js_module }}.{{ dom_id }}.addOverlay({{ js_module }}.{{ dom_id }}_polyline{{ forloop.counter }}); 25 {% for event in polyline.events %}GEvent.addListener({{ js_module }}.{{ dom_id }}_polyline{{ forloop.parentloop.counter }}, {{ event }}); {% endfor %} 26 {% if calc_zoom %}tmp_bounds = {{ js_module }}.{{ dom_id }}_polyline{{ forloop.counter }}.getBounds(); bounds.extend(tmp_bounds.getSouthWest()); bounds.extend(tmp_bounds.getNorthEast());{% endif %}{% endfor %} 27 {% for marker in markers %}{{ js_module }}.{{ dom_id }}_marker{{ forloop.counter }} = new {{ marker }}; 28 {{ js_module }}.{{ dom_id }}.addOverlay({{ js_module }}.{{ dom_id }}_marker{{ forloop.counter }}); 29 {% for event in marker.events %}GEvent.addListener({{ js_module }}.{{ dom_id }}_marker{{ forloop.parentloop.counter }}, {{ event }}); {% endfor %} 30 {% if calc_zoom %}bounds.extend({{ js_module }}.{{ dom_id }}_marker{{ forloop.counter }}.getLatLng()); {% endif %}{% endfor %} 31 {% if calc_zoom %}{{ js_module }}.{{ dom_id }}.setCenter(bounds.getCenter(), {{ js_module }}.{{ dom_id }}.getBoundsZoomLevel(bounds));{% endif %} 32 {% block load_extra %}{% endblock %} 33 }else { 34 alert("Sorry, the Google Maps API is not compatible with this browser."); 35 } 7 {{ js_module }}.{{ dom_id }} = new google.maps.Map(document.getElementById("{{ dom_id }}")); 8 {{ js_module }}.{{ dom_id }}.setCenter(new google.maps.LatLng({{ center.1 }}, {{ center.0 }})); 9 {{ js_module }}.{{ dom_id }}.setZoom({{ zoom }}); 10 {% block controls %}{% endblock %} 11 {% if calc_zoom %}var bounds = new google.maps.LatLngBounds(); var i; var j;{% endif %} 12 {% for kml_url in kml_urls %}{{ js_module }}.{{ dom_id }}_kml{{ forloop.counter }} = new google.maps.KmlLayer("{{ kml_url }}"); 13 {{ js_module }}.{{ dom_id }}_kml{{ forloop.counter }}.setMap({{ js_module }}.{{ dom_id }});{% endfor %} 14 {% for polygon in polygons %}{{ js_module }}.{{ dom_id }}_poly{{ forloop.counter }} = new {{ polygon }}; 15 {{ js_module }}.{{ dom_id }}_poly{{ forloop.counter }}.setMap({{ js_module }}.{{ dom_id }}); 16 {% for event in polygon.events %}google.event.addListener({{ js_module }}.{{ dom_id }}_poly{{ forloop.parentloop.counter }}, {{ event }});{% endfor %} 17 {% if calc_zoom %}paths={{ js_module }}.{{ dom_id }}_polyline{{ forloop.counter }}.getPaths(); for(i=0; i<paths.length(); i++){ for(j=0; j<paths[i].length(); j++){ bounds.extend(paths[i][j]); }}{% endif %}{% endfor %} 18 {% for polyline in polylines %}{{ js_module }}.{{ dom_id }}_polyline{{ forloop.counter }} = new {{ polyline }}; 19 {{ js_module }}.{{ dom_id }}.addOverlay({{ js_module }}.{{ dom_id }}_polyline{{ forloop.counter }}); 20 {% for event in polyline.events %}GEvent.addListener({{ js_module }}.{{ dom_id }}_polyline{{ forloop.parentloop.counter }}, {{ event }}); {% endfor %} 21 {% if calc_zoom %}path={{ js_module }}.{{ dom_id }}_polyline{{ forloop.counter }}.getPath(); for(i=0; i<path.length(); i++){ bounds.extend(path[i]); }{% endif %}{% endfor %} 22 {% for marker in markers %}{{ js_module }}.{{ dom_id }}_marker{{ forloop.counter }} = new {{ marker }}; 23 {{ js_module }}.{{ dom_id }}.addOverlay({{ js_module }}.{{ dom_id }}_marker{{ forloop.counter }}); 24 {% for event in marker.events %}GEvent.addListener({{ js_module }}.{{ dom_id }}_marker{{ forloop.parentloop.counter }}, {{ event }}); {% endfor %} 25 {% if calc_zoom %}bounds.extend({{ js_module }}.{{ dom_id }}_marker{{ forloop.counter }}.getPosition());{% endif %}{% endfor %} 26 {% if calc_zoom %}{{ js_module }}.{{ dom_id }}.fitBounds(bounds);{% endif %} 27 {% block load_extra %}{% endblock %} 36 28 } 37 29 {% endblock load %}{% endblock functions %}{% endlocalize %}{% endautoescape %} -
django/contrib/gis/maps/google/gmap.py
8 8 9 9 # The default Google Maps URL (for the API javascript) 10 10 # TODO: Internationalize for Japan, UK, etc. 11 GOOGLE_MAPS_URL='http://maps.google.com/maps ?file=api&v=%s&key='11 GOOGLE_MAPS_URL='http://maps.google.com/maps/api/js?sensor=%s' 12 12 13 13 class GoogleMap(object): 14 14 "A class for generating Google Maps JavaScript." 15 15 16 16 # String constants 17 onunload = mark_safe(' onunload="GUnload()"') # Cleans up after Google Maps17 onunload = mark_safe('') # NOTE: Used to call GUnload, but google maps v3 doesn't need it so now blank 18 18 vml_css = mark_safe('v\:* {behavior:url(#default#VML);}') # CSS for IE VML 19 19 xmlns = mark_safe('xmlns:v="urn:schemas-microsoft-com:vml"') # XML Namespace (for IE VML). 20 20 21 def __init__(self, key=None, api_url=None, version=None, 22 center=None, zoom=None, dom_id='map', 21 def __init__(self, api_url=None, center=None, zoom=None, dom_id='map', 23 22 kml_urls=[], polylines=None, polygons=None, markers=None, 23 using_sensor=False, 24 24 template='gis/google/google-map.js', 25 25 js_module='geodjango', 26 26 extra_context={}): 27 27 28 # The Google Maps API Key defined in the settings will be used29 # if not passed in as a parameter. The use of an API key is30 # _required_.31 if not key:32 try:33 self.key = settings.GOOGLE_MAPS_API_KEY34 except AttributeError:35 raise GoogleMapException('Google Maps API Key not found (try adding GOOGLE_MAPS_API_KEY to your settings).')36 else:37 self.key = key38 39 # Getting the Google Maps API version, defaults to using the latest ("2.x"),40 # this is not necessarily the most stable.41 if not version:42 self.version = getattr(settings, 'GOOGLE_MAPS_API_VERSION', '2.x')43 else:44 self.version = version45 46 28 # Can specify the API URL in the `api_url` keyword. 47 29 if not api_url: 48 self.api_url = mark_safe(getattr(settings, 'GOOGLE_MAPS_URL', GOOGLE_MAPS_URL) % self.version) 30 sensor_value = 'false' # maps v3 needs to know if the device rendering the page has a gps sensor 31 if using_sensor: sensor_value = 'true' 32 self.api_url = mark_safe(getattr(settings, 'GOOGLE_MAPS_URL', GOOGLE_MAPS_URL) % sensor_value) 49 33 else: 50 34 self.api_url = api_url 51 35 … … 118 102 @property 119 103 def api_script(self): 120 104 "Returns the <script> tag for the Google Maps API javascript." 121 return mark_safe('<script src="%s %s" type="text/javascript"></script>' % (self.api_url, self.key))105 return mark_safe('<script src="%s" type="text/javascript"></script>' % self.api_url) 122 106 123 107 @property 124 108 def js(self): -
django/contrib/gis/maps/google/overlays.py
3 3 4 4 class GEvent(object): 5 5 """ 6 A Python wrapper for the Google GEvent object. 6 A Python wrapper for wiring map events using the 7 google.maps.event.addListener() function. 7 8 8 9 Events can be attached to any object derived from GOverlayBase with the 9 10 add_event() call. 10 11 11 12 For more information please see the Google Maps API Reference: 12 http://code.google.com/apis/maps/documentation/ reference.html#GEvent13 http://code.google.com/apis/maps/documentation/javascript/reference.html#event 13 14 14 15 Example: 15 16 … … 54 55 self.events = [] 55 56 56 57 def latlng_from_coords(self, coords): 57 "Generates a JavaScript array of GLatLng objects for the given coordinates."58 return '[%s]' % ','.join(['new GLatLng(%s,%s)' % (y, x) for x, y in coords])58 "Generates a JavaScript array of google.maps.LatLng objects for the given coordinates." 59 return '[%s]' % ','.join(['new google.maps.LatLng(%s,%s)' % (y, x) for x, y in coords]) 59 60 60 61 def add_event(self, event): 61 " Attaches a GEvent to the overlay object."62 "Causes the event to be applied to the overlay object" 62 63 self.events.append(event) 63 64 64 65 def __unicode__(self): … … 67 68 68 69 class GPolygon(GOverlayBase): 69 70 """ 70 A Python wrapper for the Google GPolygon object. For more information71 A Python wrapper for the google.maps.Polygon object. For more information 71 72 please see the Google Maps API Reference: 72 http://code.google.com/apis/maps/documentation/ reference.html#GPolygon73 http://code.google.com/apis/maps/documentation/javascript/reference.html#Polygon 73 74 """ 74 75 def __init__(self, poly, 75 76 stroke_color='#0000ff', stroke_weight=2, stroke_opacity=1, 76 77 fill_color='#0000ff', fill_opacity=0.4): 77 78 """ 78 The GPolygon object initializes on a GEOS Polygon or a parameter that79 may be instantiated into GEOS Polygon. Please note that this will not80 depict a Polygon's internal rings.79 The GPolygon object initializes on a GEOS Polygon or a 80 parameter that may be instantiated into GEOS Polygon. Please note 81 that this will not depict a Polygon's internal rings. 81 82 82 83 Keyword Options: 83 84 … … 119 120 120 121 @property 121 122 def js_params(self): 122 return '%s, "%s", %s, %s, "%s", %s' % (self.points, self.stroke_color, self.stroke_weight, self.stroke_opacity, 123 self.fill_color, self.fill_opacity) 123 result = [] 124 result.append('paths: %s' % self.points) 125 if self.stroke_color: result.append('strokeColor: "%s"' % self.stroke_color) 126 if self.stroke_weight: result.append('strokeWeight: "%s"' % self.stroke_weight) 127 if self.stroke_opacity: result.append('strokeOpacity: "%s"' % self.stroke_opacity) 128 if self.fill_color: result.append('fillColor: "%s"' % self.fill_color) 129 if self.fill_opacity: result.append('fillOpactiy: "%s"' % self.fill_opacity) 130 return '{%s}' % ','.join(result) 124 131 125 132 class GPolyline(GOverlayBase): 126 133 """ 127 A Python wrapper for the Google GPolyline object. For more information134 A Python wrapper for the google.maps.Polyline object. For more information 128 135 please see the Google Maps API Reference: 129 http://code.google.com/apis/maps/documentation/ reference.html#GPolyline136 http://code.google.com/apis/maps/documentation/javascript/reference.html#Polyline 130 137 """ 131 138 def __init__(self, geom, color='#0000ff', weight=2, opacity=1): 132 139 """ … … 163 170 164 171 @property 165 172 def js_params(self): 166 return '%s, "%s", %s, %s' % (self.latlngs, self.color, self.weight, self.opacity) 173 result = [] 174 result.append('path: %s' % self.latlngs) 175 if self.color: result.append('strokeColor: "%s"' % self.color) 176 if self.weight: result.append('strokeWeight: "%s"' % self.weight) 177 if self.opacity: result.append('strokeOpacity: "%s"' % self.opacity) 178 return '{%s}' % ','.join(result) 167 179 168 180 169 class GI con(object):181 class GImage(object): 170 182 """ 171 Creates a GIcon object to pass into a Gmarker object. 183 Creates a GImage object to pass into a Gmarker object for the icon 184 and shadow arguments. The arguments are used to create a MarkerImage 185 class in the javascript: 172 186 173 The keyword arguments map to instance attributes of the same name. These, 174 in turn, correspond to a subset of the attributes of the official GIcon 175 javascript object: 187 http://code.google.com/apis/maps/documentation/javascript/reference.html#MarkerImage 176 188 177 http://code.google.com/apis/maps/documentation/reference.html#GIcon178 179 Because a Google map often uses several different icons, a name field has180 been added to the required arguments.181 182 189 Required Arguments: 183 varname: 184 A string which will become the basis for the js variable name of 185 the marker, for this reason, your code should assign a unique 186 name for each GIcon you instantiate, otherwise there will be 187 name space collisions in your javascript. 190 191 url: 192 The url of the image to be used as the icon on the map 188 193 189 194 Keyword Options: 190 image:191 The url of the image to be used as the icon on the map defaults192 to 'G_DEFAULT_ICON'193 195 194 iconsize:196 size: 195 197 a tuple representing the pixel size of the foreground (not the 196 198 shadow) image of the icon, in the format: (width, height) ex.: 197 199 198 GIcon('fast_food', 199 image="/media/icon/star.png", 200 GIcon("/media/icon/star.png", 200 201 iconsize=(15,10)) 201 202 202 203 Would indicate your custom icon was 15px wide and 10px height. 203 204 shadow: 205 the url of the image of the icon's shadow 206 207 shadowsize: 208 a tuple representing the pixel size of the shadow image, format is 209 the same as ``iconsize`` 210 211 iconanchor: 204 205 origin: 206 a tuple representing the pixel coordinate of the upper left corner 207 of the icon. Used in conjuction with the size option to specify 208 the sprite/subset of an image. In the format: (x,y) ex.: 209 210 3rd_marker = GIcon("/media/icon/12_markers.png", 211 size=(15,10), 212 origin=(30,0)) 213 214 Would indicate the image where it's upper left corner is at (30,0) 215 and its lower right corner is (45,10). 216 217 anchor: 212 218 a tuple representing the pixel coordinate relative to the top left 213 219 corner of the icon image at which this icon is anchored to the map. 214 220 In (x, y) format. x increases to the right in the Google Maps 215 221 coordinate system and y increases downwards in the Google Maps 216 222 coordinate system.) 217 223 218 infowindowanchor:219 The pixel coordinate relative to the top left corner of the icon220 image at which the info window is anchored to this icon.221 222 224 """ 223 def __init__(self, varname, image=None, iconsize=None, 224 shadow=None, shadowsize=None, iconanchor=None, 225 infowindowanchor=None): 226 self.varname = varname 227 self.image = image 228 self.iconsize = iconsize 229 self.shadow = shadow 230 self.shadowsize = shadowsize 231 self.iconanchor = iconanchor 232 self.infowindowanchor = infowindowanchor 225 def __init__(self, url, size=None, origin=None, anchor=None): 226 self.url = url 227 self.size = size 228 self.origin = origin 229 self.anchor = anchor 233 230 234 def __cmp__(self, other): 235 return cmp(self.varname, other.varname) 236 237 def __hash__(self): 238 # XOR with hash of GIcon type so that hash('varname') won't 239 # equal hash(GIcon('varname')). 240 return hash(self.__class__) ^ hash(self.varname) 231 def _to_param(self): 232 args = "(%s" % self.url 233 if self.size: 234 args += ", new google.maps.Size(%s)" % self.size 235 if self.origin: 236 args += ", new google.maps.Point(%s)" % self.origin 237 if self.anchor: 238 args += ", new google.maps.Point(%s)" % self.anchor 239 args += ")" 240 return "new google.maps.MarkerImage(%s)" % args 241 241 242 242 class GMarker(GOverlayBase): 243 243 """ 244 244 A Python wrapper for the Google GMarker object. For more information 245 245 please see the Google Maps API Reference: 246 http://code.google.com/apis/maps/documentation/ reference.html#GMarker246 http://code.google.com/apis/maps/documentation/javascript/reference.html#Marker 247 247 248 248 Example: 249 249 … … 258 258 return render_to_response('mytemplate.html', 259 259 {'google' : GoogleMap(markers=[marker])}) 260 260 """ 261 def __init__(self, geom, title=None, draggable=False, icon=None ):261 def __init__(self, geom, title=None, draggable=False, icon=None, shadow=None): 262 262 """ 263 263 The GMarker object may initialize on GEOS Points or a parameter 264 that may be instantiated into a GEOS point. Keyword options map to 265 GMarkerOptions -- so far only the title option is supported. 264 that may be instantiated into a GEOS point. 266 265 267 266 Keyword Options: 268 267 title: … … 270 269 271 270 draggable: 272 271 Draggable option for GMarker, disabled by default. 272 273 icon: 274 Sets the GIcon used to display the marker on the map. 275 If not set google maps will use the default marker icon. 276 277 shadow: 278 Sets the GIcon used to display the shadow of the marker on the map. 273 279 """ 274 280 # If a GEOS geometry isn't passed in, try to construct one. 275 281 if isinstance(geom, basestring): geom = fromstr(geom) … … 284 290 self.title = title 285 291 self.draggable = draggable 286 292 self.icon = icon 293 self.shadow = shadow 287 294 super(GMarker, self).__init__() 288 295 289 296 def latlng_from_coords(self, coords): 290 return 'new GLatLng(%s,%s)' %(coords[1], coords[0])297 return 'new google.maps.LatLng(%s,%s)' %(coords[1], coords[0]) 291 298 292 def options(self): 299 @property 300 def js_params(self): 293 301 result = [] 302 result.append('position: %s' % self.latlng) 294 303 if self.title: result.append('title: "%s"' % self.title) 295 if self.icon: result.append('icon: %s' % self.icon.varname) 304 if self.icon: result.append('icon: %s' % self.icon._to_param()) 305 if self.shadow: result.append('shadow: %s' % self.shadow_to_param()) 296 306 if self.draggable: result.append('draggable: true') 297 307 return '{%s}' % ','.join(result) 298 299 @property300 def js_params(self):301 return '%s, %s' % (self.latlng, self.options())