Ticket #9204: more_complete_gicon_patch.diff
File more_complete_gicon_patch.diff, 9.3 KB (added by , 16 years ago) |
---|
-
templates/gis/google/js/google-map.js
20 20 {% for event in polyline.events %}GEvent.addListener(polyline{{ forloop.parentloop.counter }}, {{ event }}); {% endfor %} 21 21 {% if calc_zoom %}tmp_bounds = polyline{{ forloop.counter }}.getBounds(); bounds.extend(tmp_bounds.getSouthWest()); bounds.extend(tmp_bounds.getNorthEast());{% endif %}{% endfor %} 22 22 23 {% for icon in icons %} 24 var {{ icon.name }}_icon = new GIcon(G_DEFAULT_ICON); 25 {% if icon.image %}{{ icon.name }}_icon.image = "{{ icon.image }}";{% endif %} 26 {% if icon.shadow %}{{ icon.name }}_icon.shadow = "{{ icon.shadow }}";{% endif %} 27 {% if icon.iconsize %}{{ icon.name }}_icon.iconSize = new GSize({{ icon.iconsize.0 }}, {{ icon.iconsize.1 }});{% endif %} 28 {% if icon.shadowsize %}{{ icon.name }}_icon.shadowSize = new GSize({{ icon.shadowsize.0 }}, {{ icon.shadowsize.1 }});{% endif %} 29 {% if icon.iconanchor %}{{ icon.name }}_icon.iconAnchor = new GPoint({{ icon.iconanchor.0 }}, {{ icon.iconanchor.1 }});{% endif %} 30 {% if icon.infowindowanchor %}{{ icon.name }}_icon.infoWindowAnchor = new GPoint({{ icon.infowindowanchor.0 }}, {{ icon.infowindowanchor.1 }});{% endif %}{% endfor %} 31 23 32 {% for marker in markers %}var marker{{ forloop.counter }} = new {{ marker }}; 24 33 map.addOverlay(marker{{ forloop.counter }}); 25 34 {% for event in marker.events %}GEvent.addListener(marker{{ forloop.parentloop.counter }}, {{ event }}); {% endfor %} -
maps/google/gmap.py
4 4 from django.utils.safestring import mark_safe 5 5 6 6 class GoogleMapException(Exception): pass 7 from django.contrib.gis.maps.google.overlays import GPolygon, GPolyline, GMarker 7 from django.contrib.gis.maps.google.overlays import GPolygon, GPolyline, \ 8 GMarker, GIcon 8 9 9 10 # The default Google Maps URL (for the API javascript) 10 11 # TODO: Internationalize for Japan, UK, etc. … … 20 21 21 22 def __init__(self, key=None, api_url=None, version=None, 22 23 center=None, zoom=None, dom_id='map', load_func='gmap_load', 23 kml_urls=[], polygons=[], polylines=[], markers=[],24 template='gis/google/js/google-map.js',24 kml_urls=[], markers=None, icons=None, polygons=None, 25 polylines=None, template='gis/google/js/google-map.js', 25 26 extra_context={}): 26 27 27 28 # The Google Maps API Key defined in the settings will be used … … 56 57 self.kml_urls = kml_urls 57 58 58 59 # Does the user want any GMarker, GPolygon, and/or GPolyline overlays? 59 self.polygons, self.polylines, self.markers = [], [], [] 60 if markers: 61 for point in markers: 62 if isinstance(point, GMarker): 63 self.markers.append(point) 64 else: 65 self.markers.append(GMarker(point)) 66 if polygons: 67 for poly in polygons: 68 if isinstance(poly, GPolygon): 69 self.polygons.append(poly) 70 else: 71 self.polygons.append(GPolygon(poly)) 72 if polylines: 73 for pline in polylines: 74 if isinstance(pline, GPolyline): 75 self.polylines.append(pline) 76 else: 77 self.polylines.append(GPolyline(pline)) 78 60 overlay_info = [[GIcon, icons, 'icons'], 61 [GMarker, markers, 'markers'], 62 [GPolygon, polygons, 'polygons'], 63 [GPolyline, polylines, 'polylines']] 64 65 for overlay_class, overlay_list, varname in overlay_info: 66 setattr(self, varname, []) 67 if overlay_list: 68 for overlay in overlay_list: 69 if isinstance(overlay, overlay_class): 70 getattr(self, varname).append(overlay) 71 else: 72 getattr(self, varname).append(overlay_class(overlay)) 73 79 74 # If GMarker, GPolygons, and/or GPolylines 80 75 # are used the zoom will be automatically 81 76 # calculated via the Google Maps API. If both a zoom level and a … … 103 98 'polygons' : self.polygons, 104 99 'polylines' : self.polylines, 105 100 'markers' : self.markers, 101 'icons' : self.icons, 106 102 } 107 103 params.update(extra_context) 108 104 self.js = render_to_string(self.template, params) -
maps/google/overlays.py
165 165 def js_params(self): 166 166 return '%s, "%s", %s, %s' % (self.latlngs, self.color, self.weight, self.opacity) 167 167 168 class GIcon(object): 169 """ 170 Creates a GIcon object to pass into a Gmarker object. 171 172 The keyword arguments map to instance attributes of the same name. These, 173 in turn, correspond to a subset of the attributes of the official GIcon 174 javascript object: 175 176 http://code.google.com/apis/maps/documentation/reference.html#GIcon 177 178 Because a Google map often uses several different icons, a name field has 179 been added to the required arguments. 180 181 Required Arguments: 182 name: 183 A string which will become the basis for the js variable name of 184 the marker, for this reason, your code should assign a unique 185 name for each GIcon you instantiate, otherwise there will be 186 name space collisions in your javascript. 187 188 Keyword Options: 189 image: 190 The url of the image to be used as the icon on the map defaults 191 to 'G_DEFAULT_ICON' 192 193 iconsize: 194 a tuple representing the pixel size of the foreground (not the 195 shadow) image of the icon, in the format: (width, height) ex.: 196 197 GIcon('fast_food', 198 image="/media/icon/star.png", 199 iconsize=(15,10)) 200 201 Would indicate your custom icon was 15px wide and 10px height. 202 203 shadow: 204 the url of the image of the icon's shadow 205 206 shadowsize: 207 a tuple representing the pixel size of the shadow image, format is 208 the same as ``iconsize`` 209 210 iconanchor: 211 a tuple representing the pixel coordinate relative to the top left 212 corner of the icon image at which this icon is anchored to the map. 213 In (x, y) format. x increases to the right in the Google Maps 214 coordinate system and y increases downwards in the Google Maps 215 coordinate system.) 216 217 infowindowanchor: 218 The pixel coordinate relative to the top left corner of the icon 219 image at which the info window is anchored to this icon. 220 221 222 """ 223 def __init__(self, name, image='G_DEFAULT_ICON', iconsize=None, 224 shadow=None, shadowsize=None, iconanchor=None, 225 infowindowanchor=None): 226 self.name = name 227 self.image = image 228 self.iconsize = iconsize 229 self.shadow = shadow 230 self.shadowsize = shadowsize 231 self.iconanchor = iconanchor 232 self.infowindowanchor = infowindowanchor 233 234 168 235 class GMarker(GOverlayBase): 169 236 """ 170 237 A Python wrapper for the Google GMarker object. For more information … … 184 251 return render_to_response('mytemplate.html', 185 252 {'google' : GoogleMap(markers=[marker])}) 186 253 """ 187 def __init__(self, geom, title=None ):254 def __init__(self, geom, title=None, draggable=False, icon=None): 188 255 """ 189 256 The GMarker object may initialize on GEOS Points or a parameter 190 257 that may be instantiated into a GEOS point. Keyword options map to … … 193 260 Keyword Options: 194 261 title: 195 262 Title option for GMarker, will be displayed as a tooltip. 263 264 draggable: 265 Draggable option for GMarker, disabled by default 266 267 icon: 268 Sets a custom icon for the marker 196 269 """ 197 270 # If a GEOS geometry isn't passed in, try to construct one. 198 271 if isinstance(geom, basestring): geom = fromstr(geom) … … 205 278 self.envelope = geom.envelope 206 279 # TODO: Add support for more GMarkerOptions 207 280 self.title = title 281 self.draggable = draggable 282 self.icon = icon 208 283 super(GMarker, self).__init__() 209 284 210 285 def latlng_from_coords(self, coords): … … 212 287 213 288 def options(self): 214 289 result = [] 215 if self.title: result.append('title: "%s"' % self.title) 290 if self.title: result.append('title: "%s"' % self.title) 291 if self.icon: result.append('icon: %s_icon' % self.icon.name) 292 if self.draggable: result.append('draggable: true') 216 293 return '{%s}' % ','.join(result) 217 294 218 295 @property