Ticket #9204: global_iconvars_gicon_patch.diff

File global_iconvars_gicon_patch.diff, 8.4 KB (added by prairiedogg, 15 years ago)

Moved icon variable definition into the base google maps js template alongside the other global variables.

  • templates/gis/google/google-base.js

     
    1 {% block vars %}var geodjango = {};{% endblock %}
     1{% block vars %}var geodjango = {};{% for icon in icons %}
     2var {{ icon.name }}_icon = new GIcon(G_DEFAULT_ICON);
     3{% if icon.image %}{{ icon.name }}_icon.image = "{{ icon.image }}";{% endif %}
     4{% if icon.shadow %}{{ icon.name }}_icon.shadow = "{{ icon.shadow }}";{% endif %}
     5{% if icon.iconsize %}{{ icon.name }}_icon.iconSize = new GSize({{ icon.iconsize.0 }}, {{ icon.iconsize.1 }});{% endif %}
     6{% if icon.shadowsize %}{{ icon.name }}_icon.shadowSize = new GSize({{ icon.shadowsize.0 }}, {{ icon.shadowsize.1 }});{% endif %}
     7{% if icon.iconanchor %}{{ icon.name }}_icon.iconAnchor = new GPoint({{ icon.iconanchor.0 }}, {{ icon.iconanchor.1 }});{% endif %}
     8{% if icon.infowindowanchor %}{{ icon.name }}_icon.infoWindowAnchor = new GPoint({{ icon.infowindowanchor.0 }}, {{ icon.infowindowanchor.1 }});{% endif %}{% endfor %}{% endblock %}
    29{% block functions %}{% endblock %}
     10 No newline at end of file
  • maps/google/gmap.py

     
    44from django.utils.safestring import mark_safe
    55
    66class GoogleMapException(Exception): pass
    7 from django.contrib.gis.maps.google.overlays import GPolygon, GPolyline, GMarker
    8 
     7from django.contrib.gis.maps.google.overlays import GPolygon, GPolyline, \
     8                                                    GMarker, GIcon
     9 
    910# The default Google Maps URL (for the API javascript)
    1011# TODO: Internationalize for Japan, UK, etc.
    1112GOOGLE_MAPS_URL='http://maps.google.com/maps?file=api&v=%s&key='
     
    2021
    2122    def __init__(self, key=None, api_url=None, version=None,
    2223                 center=None, zoom=None, dom_id='map',
    23                  kml_urls=[], polygons=[], polylines=[], markers=[],
    24                  template='gis/google/google-single.js',
     24                 kml_urls=[], icons=None, markers=None, polylines=None,
     25                 polygons=None, template='gis/google/google-single.js',
    2526                 js_module='geodjango',extra_context={}):
    2627
    2728        # The Google Maps API Key defined in the settings will be used
     
    5758        self.kml_urls = kml_urls
    5859       
    5960        # Does the user want any GMarker, GPolygon, and/or GPolyline overlays?
    60         self.polygons, self.polylines, self.markers = [], [], []
    61         if markers:
    62             for point in markers:
    63                 if isinstance(point, GMarker):
    64                     self.markers.append(point)
    65                 else:
    66                     self.markers.append(GMarker(point))
    67         if polygons:
    68             for poly in polygons:
    69                 if isinstance(poly, GPolygon):
    70                     self.polygons.append(poly)
    71                 else:
    72                     self.polygons.append(GPolygon(poly))
    73         if polylines:
    74             for pline in polylines:
    75                 if isinstance(pline, GPolyline):
    76                     self.polylines.append(pline)
    77                 else:
    78                     self.polylines.append(GPolyline(pline))
    79        
     61        overlay_info = [[GIcon, icons, 'icons'],
     62                        [GMarker, markers, 'markers'],
     63                        [GPolygon, polygons, 'polygons'],
     64                        [GPolyline, polylines, 'polylines']]
     65
     66        for overlay_class, overlay_list, varname in overlay_info:
     67            setattr(self, varname, [])
     68            if overlay_list:
     69                for overlay in overlay_list:
     70                    if isinstance(overlay, overlay_class):
     71                        getattr(self, varname).append(overlay)
     72                    else:
     73                        getattr(self, varname).append(overlay_class(overlay))
     74
    8075        # If GMarker, GPolygons, and/or GPolylines are used the zoom will be
    8176        # automatically calculated via the Google Maps API.  If both a zoom
    8277        # level and a center coordinate are provided with polygons/polylines,
     
    105100                  'zoom' : self.zoom,
    106101                  'polygons' : self.polygons,
    107102                  'polylines' : self.polylines,
     103                  'icons': self.icons,
    108104                  'markers' : self.markers,
    109105                  }
    110106        params.update(self.extra_context)
  • maps/google/overlays.py

     
    165165    def js_params(self):
    166166        return '%s, "%s", %s, %s' % (self.latlngs, self.color, self.weight, self.opacity)
    167167
     168
     169class GIcon(object):
     170    """
     171    Creates a GIcon object to pass into a Gmarker object.
     172
     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:
     176
     177    http://code.google.com/apis/maps/documentation/reference.html#GIcon
     178
     179    Because a Google map often uses several different icons, a name field has
     180    been added to the required arguments.
     181
     182    Required Arguments:
     183        name: 
     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.
     188
     189    Keyword Options:
     190        image:
     191            The url of the image to be used as the icon on the map defaults 
     192            to 'G_DEFAULT_ICON'
     193
     194        iconsize:
     195            a tuple representing the pixel size of the foreground (not the 
     196            shadow) image of the icon, in the format: (width, height) ex.:
     197
     198            GIcon('fast_food', 
     199                  image="/media/icon/star.png",
     200                  iconsize=(15,10))
     201
     202            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:
     212            a tuple representing the pixel coordinate relative to the top left 
     213            corner of the icon image at which this icon is anchored to the map.
     214            In (x, y) format.  x increases to the right in the Google Maps
     215            coordinate system and y increases downwards in the Google Maps 
     216            coordinate system.)
     217
     218        infowindowanchor:
     219            The pixel coordinate relative to the top left corner of the icon
     220            image at which the info window is anchored to this icon.   
     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
    168235class GMarker(GOverlayBase):
    169236    """
    170237    A Python wrapper for the Google GMarker object.  For more information
     
    184251          return render_to_response('mytemplate.html',
    185252                 {'google' : GoogleMap(markers=[marker])})
    186253    """
    187     def __init__(self, geom, title=None, draggable=False):
     254    def __init__(self, geom, title=None, draggable=False, icon=None):
    188255        """
    189256        The GMarker object may initialize on GEOS Points or a parameter
    190257        that may be instantiated into a GEOS point.  Keyword options map to
     
    209276        # TODO: Add support for more GMarkerOptions
    210277        self.title = title
    211278        self.draggable = draggable
     279        self.icon = icon
    212280        super(GMarker, self).__init__()
    213281
    214282    def latlng_from_coords(self, coords):
     
    217285    def options(self):
    218286        result = []
    219287        if self.title: result.append('title: "%s"' % self.title)
     288        if self.icon: result.append('icon: %s_icon' % self.icon.name)
    220289        if self.draggable: result.append('draggable: true')
    221290        return '{%s}' % ','.join(result)
     291   
    222292
    223293    @property
    224294    def js_params(self):
Back to Top