Ticket #9204: icons_and_draggable.diff

File icons_and_draggable.diff, 5.8 KB (added by prairiedogg, 16 years ago)

GIcon wrapper class with the draggable markers bit from #10072

  • 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 }});{% endif %}
     28    {% if icon.shadowsize %}{{ icon.name }}_icon.shadowSize = new GSize({{ icon.shadowsize }});{% endif %}
     29    {% if icon.iconanchor %}{{ icon.name }}_icon.iconAnchor = new GPoint({{ icon.iconanchor }});{% endif %}
     30    {% if icon.infowindowanchor %}{{ icon.name }}_icon.infoWindowAnchor = new GPoint({{ icon.infowindowanchor }});{% 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                 kml_urls=[], polygons=[], polylines=[], markers=[], icons=[],
    2425                 template='gis/google/js/google-map.js',
    2526                 extra_context={}):
    2627
     
    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        self.polygons, self.polylines, self.markers, self.icons = [], [], [], []
     61        if icons:
     62            for icon in icons:
     63                if isinstance(icon, GIcon):
     64                    self.icons.append(icon)
     65                else:
     66                    self.icons.append(GIcon(icon))
    6067        if markers:
    6168            for point in markers:
    6269                if isinstance(point, GMarker):
     
    103110                  'polygons' : self.polygons,
    104111                  'polylines' : self.polylines,
    105112                  'markers' : self.markers,
     113                  'icons' : self.icons,
    106114                  }
    107115        params.update(extra_context)
    108116        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    def __init__(self, name='custom', image=None, iconsize=None, shadow=None, shadowsize=None,
     173                 iconanchor=None, infowindowanchor=None):
     174        self.name = name
     175        self.image = image
     176        self.iconsize = iconsize
     177        self.shadow = shadow
     178        self.shadowsize = shadowsize
     179        self.iconanchor = iconanchor
     180        self.infowindowanchor = infowindowanchor
     181       
     182
    168183class GMarker(GOverlayBase):
    169184    """
    170185    A Python wrapper for the Google GMarker object.  For more information
     
    184199          return render_to_response('mytemplate.html',
    185200                 {'google' : GoogleMap(markers=[marker])})
    186201    """
    187     def __init__(self, geom, title=None):
     202    def __init__(self, geom, title=None, draggable=False, icon=None):
    188203        """
    189204        The GMarker object may initialize on GEOS Points or a parameter
    190205        that may be instantiated into a GEOS point.  Keyword options map to
     
    193208        Keyword Options:
    194209         title:
    195210           Title option for GMarker, will be displayed as a tooltip.
     211         
     212         draggable:
     213           Draggable option for GMarker, disabled by default
     214         
     215         icon:
     216           Takes a
    196217        """
    197218        # If a GEOS geometry isn't passed in, try to construct one.
    198219        if isinstance(geom, basestring): geom = fromstr(geom)
     
    205226        self.envelope = geom.envelope
    206227        # TODO: Add support for more GMarkerOptions
    207228        self.title = title
     229        self.draggable = draggable
     230        self.icon = icon
    208231        super(GMarker, self).__init__()
    209232
    210233    def latlng_from_coords(self, coords):
     
    212235   
    213236    def options(self):
    214237        result = []
    215         if self.title: result.append('title: "%s"' % self.title)
     238        if self.title: result.append('title: "%s"' % self.title)
     239        if self.icon: result.append('icon: %s_icon' % self.icon.name)
     240        if self.draggable: result.append('draggable: true')
    216241        return '{%s}' % ','.join(result)
    217242
    218243    @property
Back to Top