Ticket #9204: more_complete_gicon_patch.diff

File more_complete_gicon_patch.diff, 9.3 KB (added by prairiedogg, 15 years ago)

Improved the documentation for GIcon class and did a slight refactor inside overlays.py to reduce repetition

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

     
    2020    {% for event in polyline.events %}GEvent.addListener(polyline{{ forloop.parentloop.counter }}, {{ event }}); {% endfor %}
    2121    {% if calc_zoom %}tmp_bounds = polyline{{ forloop.counter }}.getBounds(); bounds.extend(tmp_bounds.getSouthWest()); bounds.extend(tmp_bounds.getNorthEast());{% endif %}{% endfor %}
    2222   
     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   
    2332    {% for marker in markers %}var marker{{ forloop.counter }} = new {{ marker }};
    2433    map.addOverlay(marker{{ forloop.counter }});
    2534    {% for event in marker.events %}GEvent.addListener(marker{{ forloop.parentloop.counter }}, {{ event }}); {% endfor %}
  • 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
     7from django.contrib.gis.maps.google.overlays import GPolygon, GPolyline, \
     8                                                    GMarker, GIcon
    89
    910# The default Google Maps URL (for the API javascript)
    1011# TODO: Internationalize for Japan, UK, etc.
     
    2021
    2122    def __init__(self, key=None, api_url=None, version=None,
    2223                 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',
    2526                 extra_context={}):
    2627
    2728        # The Google Maps API Key defined in the settings will be used
     
    5657        self.kml_urls = kml_urls
    5758       
    5859        # 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                           
    7974        # If GMarker, GPolygons, and/or GPolylines
    8075        # are used the zoom will be automatically
    8176        # calculated via the Google Maps API.  If both a zoom level and a
     
    10398                  'polygons' : self.polygons,
    10499                  'polylines' : self.polylines,
    105100                  'markers' : self.markers,
     101                  'icons' : self.icons,
    106102                  }
    107103        params.update(extra_context)
    108104        self.js = render_to_string(self.template, params)
  • maps/google/overlays.py

     
    165165    def js_params(self):
    166166        return '%s, "%s", %s, %s' % (self.latlngs, self.color, self.weight, self.opacity)
    167167
     168class 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
    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):
     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
     
    193260        Keyword Options:
    194261         title:
    195262           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
    196269        """
    197270        # If a GEOS geometry isn't passed in, try to construct one.
    198271        if isinstance(geom, basestring): geom = fromstr(geom)
     
    205278        self.envelope = geom.envelope
    206279        # TODO: Add support for more GMarkerOptions
    207280        self.title = title
     281        self.draggable = draggable
     282        self.icon = icon
    208283        super(GMarker, self).__init__()
    209284
    210285    def latlng_from_coords(self, coords):
     
    212287   
    213288    def options(self):
    214289        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')
    216293        return '{%s}' % ','.join(result)
    217294
    218295    @property
Back to Top