Ticket #7619: gis-googlemaps.diff

File gis-googlemaps.diff, 10.3 KB (added by ludwigbrinckmann@…, 16 years ago)

Patch

  • django/contrib/gis/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
     7from django.contrib.gis.maps.google.overlays import GPolygon, GPolyline, GMarker
    88
    99# The default Google Maps URL (for the API javascript)
    1010# TODO: Internationalize for Japan, UK, etc.
     
    2020
    2121    def __init__(self, key=None, api_url=None, version=None,
    2222                 center=None, zoom=None, dom_id='map', load_func='gmap_load',
    23                  kml_urls=[], polygons=[], polylines=[],
     23                 kml_urls=[], polygons=[], polylines=[], points=[],
    2424                 template='gis/google/js/google-map.js',
    2525                 extra_context={}):
    2626
     
    5656        self.kml_urls = kml_urls
    5757       
    5858        # Does the user want any GPolygon or GPolyline overlays?
    59         self.polygons, self.polylines = [], []
     59        self.polygons, self.polylines, self.points = [], [], []
     60        if points:
     61            for point in points:
     62                if isinstance(point, GMarker):
     63                    self.points.append(point)
     64                else:
     65                    self.points.append(GMarker(point))
    6066        if polygons:
    6167            for poly in polygons:
    6268                if isinstance(poly, GPolygon):
     
    7076                else:
    7177                    self.polylines.append(GPolyline(pline))
    7278       
    73         # If GPolygons and/or GPolylines are used the zoom will be automatically
     79        # If GPolygons and/or GPolylines and/or GPOints
     80        # are used the zoom will be automatically
    7481        # calculated via the Google Maps API.  If both a zoom level and a
    7582        # center coordinate are provided with polygons/polylines, no automatic
    7683        # determination will occur.
    7784        self.calc_zoom = False
    78         if self.polygons or self.polylines:
     85        if self.polygons or self.polylines  or self.points:
    7986            if center is None or zoom is None:
    8087                self.calc_zoom = True
    8188   
     
    95102                  'zoom' : self.zoom,
    96103                  'polygons' : self.polygons,
    97104                  'polylines' : self.polylines,
     105                  'points' : self.points,
    98106                  }
    99107        params.update(extra_context)
    100108        self.js = render_to_string(self.template, params)
  • django/contrib/gis/maps/google/overlays.py

     
    1 from django.contrib.gis.geos import LineString, LinearRing, Polygon
     1from django.contrib.gis.geos import LineString, LinearRing, Polygon, Point
    22from django.utils.safestring import mark_safe
    33
     4class GEvent(object):
     5    """A Python wrapper for the Google GEvent object. For more information
     6    please see the Google Maps API Reference:
     7    http://code.google.com/apis/maps/documentation/reference.html#GEvent
     8    Events can be attached to any object derived from GOverlayBase with the
     9    add_event() call.
     10
     11    Example:
     12
     13      from django.shortcuts import render_to_response
     14      from django.contrib.gis.maps.google.gmap import GoogleMap
     15      from django.contrib.gis.maps.google.overlays import GPolyline, GEvent
     16      from django.contrib.gis.geos import fromstr
     17
     18      def sample_request(request):
     19          polylines = []
     20          location = fromstr('LINESTRING(101 26, 112 26, 102 31)')
     21          polyline = GPolyline(location)
     22          event = GEvent('click',
     23            'function() { location.href = "http://www.google.com"}')
     24          polyline.add_event(event)
     25          polylines.append(polyline)
     26          return render_to_response('mytemplate.html',
     27          {'google' : GoogleMap(polylines=polylines)})
     28     
     29
     30    """
     31
     32    def __init__(self, event, action):
     33        """Initializes a GEvent object.
     34       
     35        Parameters:
     36
     37          event:
     38            string for the event, such as 'click'. The event must be a valid
     39            event for the object in the Google Maps API.
     40            There is no validation of the event type within Django.
     41
     42          action:
     43            string containing a Javascript function, such as
     44            'function() { location.href = "newurl";}'
     45            The string must be a valid Javascript function. Again there is no
     46            validation fo the function within Django.
     47        """
     48        self.event = event
     49        self.action = action
     50
     51    def __unicode__(self):
     52        """Returns the parameter part of a GEvent."""
     53        return mark_safe('"%s", %s' %(self.event, self.action))
     54
    455class GOverlayBase(object):
     56    def __init__(self):
     57        self.events = []
     58
    559    def latlng_from_coords(self, coords):
    660        return '[%s]' % ','.join(['new GLatLng(%s,%s)' % (y, x) for x, y in coords])
    761
     62    def add_event(self, event):
     63        """Attaches a GEvent to the overlay object."""
     64
     65        self.events.append(event)
     66
    867    def __unicode__(self):
    968        "The string representation is the JavaScript API call."
    1069        return mark_safe('%s(%s)' % (self.__class__.__name__, self.js_params))
     
    4099            The opacity of the polygon fill.  Defaults to 0.4.
    41100        """
    42101
     102        GOverlayBase.__init__(self)
     103
    43104        # TODO: Take other types of geometries.
    44105        if not isinstance(poly, Polygon):
    45106            raise TypeError('GPolygon may only initialize on GEOS Polygons.')
     
    85146          opacity:
    86147            The opacity of the polyline, between 0 and 1.  Defaults to 1.
    87148        """
     149        GOverlayBase.__init__(self)
    88150        if isinstance(geom, (LineString, LinearRing)):
    89151            self.latlngs = self.latlng_from_coords(geom.coords)
    90152        elif isinstance(geom, Polygon):
     
    99161    @property
    100162    def js_params(self):
    101163        return '%s, "%s", %s, %s' % (self.latlngs, self.color, self.weight, self.opacity)
     164
     165
     166class GMarker(GOverlayBase):
     167    """
     168    A Python wrapper for the Google GMarker object.  For more information
     169    please see the Google Maps API Reference:
     170     http://code.google.com/apis/maps/documentation/reference.html#GMarker
     171
     172    Example:
     173
     174      from django.shortcuts import render_to_response
     175      from django.contrib.gis.maps.google.gmap import GoogleMap
     176      from django.contrib.gis.maps.google.overlays import GMarker, GEvent
     177      from django.contrib.gis.geos import fromstr
     178
     179      def sample_request(request):
     180          points = []
     181          location = fromstr('POINT(101 26)')
     182          point = GMarker(location)
     183          event = GEvent('click',
     184            'function() { location.href = "http://www.google.com"}')
     185          point.add_event(event)
     186          points.append(point)
     187          return render_to_response('mytemplate.html',
     188          {'google' : GoogleMap(points=points)})
     189
     190
     191    """
     192    def __init__(self, geom, title = None):
     193        """
     194        The GMarker object may initialize on GEOS Point objects.
     195
     196          title:
     197            title option for GMarker, will be displayed as a tooltip
     198        """
     199        GOverlayBase.__init__(self)
     200        if isinstance(geom, Point):
     201            self.latlng = self.latlng_from_coords(geom.coords)
     202        else:
     203            raise TypeError('GMarker may only initialize on GEOS Point geometry.')
     204        # Getting the envelope for automatic zoom determination.
     205        self.envelope = geom.envelope
     206        self.title = title
     207
     208    def latlng_from_coords(self, coords):
     209        return 'new GLatLng(%s,%s)' %(coords[1], coords[0])
     210   
     211    def options(self):
     212        result = ''
     213        if self.title:
     214            result += 'title: "%s"' %self.title
     215        return result
     216       
     217    @property
     218    def js_params(self):
     219        return '%s, {%s}' % (self.latlng, self.options())
  • django/contrib/gis/templates/gis/google/js/google-map.js

     
    88    {% if calc_zoom %}var bounds = new GLatLngBounds(); var tmp_bounds = new GLatLngBounds();{% else %}map.setCenter(new GLatLng({{ center.1 }}, {{ center.0 }}), {{ zoom }});{% endif %}
    99    {% for kml_url in kml_urls %}var kml{{ forloop.counter }} = new GGeoXml("{{ kml_url }}");
    1010    map.addOverlay(kml{{ forloop.counter }});{% endfor %}
     11
    1112    {% for polygon in polygons %}var poly{{ forloop.counter }} = new {{ polygon }};
    12     map.addOverlay(poly{{ forloop.counter }});{% if calc_zoom %}
     13    map.addOverlay(poly{{ forloop.counter }});
     14    {% for event in polygon.events %}
     15    GEvent.addListener(poly{{ forloop.parentloop.counter }}, {{ event }}); {% endfor %}
     16    {% if calc_zoom %}
    1317    tmp_bounds = poly{{ forloop.counter }}.getBounds(); bounds.extend(tmp_bounds.getSouthWest()); bounds.extend(tmp_bounds.getNorthEast());{% endif %}{% endfor %}
     18
    1419    {% for polyline in polylines %}var polyline{{ forloop.counter }} = new {{ polyline }};
    15     map.addOverlay(polyline{{ forloop.counter }});{% if calc_zoom %}
     20    map.addOverlay(polyline{{ forloop.counter }});
     21    {% for event in polyline.events %}
     22    GEvent.addListener(polyline{{ forloop.parentloop.counter }}, {{ event }}); {% endfor %}
     23    {% if calc_zoom %}
    1624    tmp_bounds = polyline{{ forloop.counter }}.getBounds(); bounds.extend(tmp_bounds.getSouthWest()); bounds.extend(tmp_bounds.getNorthEast());{% endif %}{% endfor %}
     25
     26
     27    {% for point in points %}var point{{ forloop.counter }} = new {{ point }};
     28    {% for event in point.events %}
     29    GEvent.addListener(point{{ forloop.parentloop.counter }}, {{ event }});
     30    {% endfor %}
     31    {% if calc_zoom %}map.setCenter(new GLatLng(point{{ forloop.counter }}.getLatLng())); {% endif %}
     32    map.addOverlay(point{{ forloop.counter }});
     33    {% if calc_zoom %}bounds.extend(point{{ forloop.counter }}.getLatLng()); {% endif %}{% endfor %}
     34
    1735    {% if calc_zoom %}map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));{% endif %}
    1836    {% block load_extra %}{% endblock %}
    1937  }else {
Back to Top