Django

Code

Show
Ignore:
Timestamp:
07/05/08 17:10:33 (5 months ago)
Author:
jbronn
Message:

gis: Fixed #7619. Added support Google Maps markers (GMarker) and events (GEvent). Thanks, Ludwig Brinckmann.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/gis/django/contrib/gis/maps/google/gmap.py

    r7400 r7841  
    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) 
     
    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=[], markers=[], 
    2424                 template='gis/google/js/google-map.js', 
    2525                 extra_context={}): 
     
    5656        self.kml_urls = kml_urls 
    5757         
    58         # Does the user want any GPolygon or GPolyline overlays? 
    59         self.polygons, self.polylines = [], [] 
     58        # 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)) 
    6066        if polygons: 
    6167            for poly in polygons: 
     
    7177                    self.polylines.append(GPolyline(pline)) 
    7278        
    73         # If GPolygons and/or GPolylines are used the zoom will be automatically 
     79        # If GMarker, GPolygons, and/or GPolylines  
     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.markers
    7986            if center is None or zoom is None: 
    8087                self.calc_zoom = True 
     
    96103                  'polygons' : self.polygons, 
    97104                  'polylines' : self.polylines, 
     105                  'markers' : self.markers, 
    98106                  } 
    99107        params.update(extra_context) 
  • django/branches/gis/django/contrib/gis/maps/google/__init__.py

    r7400 r7841  
    5858""" 
    5959from django.contrib.gis.maps.google.gmap import GoogleMap 
    60 from django.contrib.gis.maps.google.overlays import GPolygon, GPolyline 
     60from django.contrib.gis.maps.google.overlays import GEvent, GMarker, GPolygon, GPolyline 
    6161from django.contrib.gis.maps.google.zoom import GoogleZoom 
  • django/branches/gis/django/contrib/gis/maps/google/overlays.py

    r7213 r7841  
    1 from django.contrib.gis.geos import LineString, LinearRing, Polygon 
    21from django.utils.safestring import mark_safe 
     2from django.contrib.gis.geos import fromstr, Point, LineString, LinearRing, Polygon 
     3 
     4class GEvent(object): 
     5    """ 
     6    A Python wrapper for the Google GEvent object. 
     7 
     8    Events can be attached to any object derived from GOverlayBase with the 
     9    add_event() call. 
     10 
     11    For more information please see the Google Maps API Reference: 
     12     http://code.google.com/apis/maps/documentation/reference.html#GEvent 
     13 
     14    Example: 
     15 
     16      from django.shortcuts import render_to_response 
     17      from django.contrib.gis.maps.google import GoogleMap, GEvent, GPolyline 
     18 
     19      def sample_request(request): 
     20          polyline = GPolyline('LINESTRING(101 26, 112 26, 102 31)') 
     21          event = GEvent('click',  
     22            'function() { location.href = "http://www.google.com"}') 
     23          polyline.add_event(event) 
     24          return render_to_response('mytemplate.html',  
     25          {'google' : GoogleMap(polylines=[polyline])}) 
     26    """ 
     27 
     28    def __init__(self, event, action): 
     29        """ 
     30        Initializes a GEvent object.  
     31         
     32        Parameters: 
     33 
     34        event:  
     35          string for the event, such as 'click'. The event must be a valid 
     36          event for the object in the Google Maps API.  
     37          There is no validation of the event type within Django. 
     38 
     39        action: 
     40          string containing a Javascript function, such as  
     41          'function() { location.href = "newurl";}' 
     42          The string must be a valid Javascript function. Again there is no  
     43          validation fo the function within Django. 
     44        """ 
     45        self.event = event 
     46        self.action = action 
     47 
     48    def __unicode__(self): 
     49        "Returns the parameter part of a GEvent." 
     50        return mark_safe('"%s", %s' %(self.event, self.action)) 
    351 
    452class GOverlayBase(object): 
     53    def __init__(self): 
     54        self.events = [] 
     55 
    556    def latlng_from_coords(self, coords): 
     57        "Generates a JavaScript array of GLatLng objects for the given coordinates." 
    658        return '[%s]' % ','.join(['new GLatLng(%s,%s)' % (y, x) for x, y in coords]) 
     59 
     60    def add_event(self, event): 
     61        "Attaches a GEvent to the overlay object." 
     62        self.events.append(event) 
    763 
    864    def __unicode__(self): 
     
    2076                 fill_color='#0000ff', fill_opacity=0.4): 
    2177        """ 
    22         The GPolygon object initializes on a GEOS Polygon.  Please note that 
    23         this will not depict Polygons with internal rings. 
     78        The GPolygon object initializes on a GEOS Polygon or a parameter that 
     79        may be instantiated into GEOS Polygon.  Please note that this will not 
     80        depict a Polygon's internal rings. 
    2481 
    2582        Keyword Options: 
     
    4097            The opacity of the polygon fill.  Defaults to 0.4. 
    4198        """ 
    42  
    43         # TODO: Take other types of geometries. 
     99        if isinstance(poly, basestring): poly = fromstr(poly) 
     100        if isinstance(poly, (tuple, list)): poly = Polygon(poly) 
    44101        if not isinstance(poly, Polygon):  
    45102            raise TypeError('GPolygon may only initialize on GEOS Polygons.') 
     
    58115        # Fill settings. 
    59116        self.fill_color, self.fill_opacity = fill_color, fill_opacity 
    60          
     117        
     118        super(GPolygon, self).__init__() 
     119  
    61120    @property 
    62121    def js_params(self): 
     
    72131    def __init__(self, geom, color='#0000ff', weight=2, opacity=1): 
    73132        """ 
    74         The GPolyline object may initialize on GEOS LineStirng, LinearRing, 
    75         and Polygon objects (internal rings not supported).   
     133        The GPolyline object may be initialized on GEOS LineStirng, LinearRing, 
     134        and Polygon objects (internal rings not supported) or a parameter that 
     135        may instantiated into one of the above geometries. 
    76136 
    77137        Keyword Options: 
     
    86146            The opacity of the polyline, between 0 and 1.  Defaults to 1. 
    87147        """ 
     148        # If a GEOS geometry isn't passed in, try to contsruct one. 
     149        if isinstance(geom, basestring): geom = fromstr(geom) 
     150        if isinstance(geom, (tuple, list)): geom = Polygon(geom) 
     151        # Generating the lat/lng coordinate pairs. 
    88152        if isinstance(geom, (LineString, LinearRing)): 
    89153            self.latlngs = self.latlng_from_coords(geom.coords) 
     
    96160        self.envelope = geom.envelope 
    97161        self.color, self.weight, self.opacity = color, weight, opacity 
     162        super(GPolyline, self).__init__() 
    98163         
    99164    @property 
    100165    def js_params(self): 
    101166        return '%s, "%s", %s, %s' % (self.latlngs, self.color, self.weight, self.opacity) 
     167 
     168class GMarker(GOverlayBase): 
     169    """ 
     170    A Python wrapper for the Google GMarker object.  For more information 
     171    please see the Google Maps API Reference: 
     172     http://code.google.com/apis/maps/documentation/reference.html#GMarker 
     173 
     174    Example: 
     175 
     176      from django.shortcuts import render_to_response 
     177      from django.contrib.gis.maps.google.overlays import GMarker, GEvent 
     178      
     179      def sample_request(request): 
     180          marker = GMarker('POINT(101 26)') 
     181          event = GEvent('click',  
     182                         'function() { location.href = "http://www.google.com"}') 
     183          marker.add_event(event) 
     184          return render_to_response('mytemplate.html',  
     185                 {'google' : GoogleMap(markers=[marker])}) 
     186    """ 
     187    def __init__(self, geom, title=None): 
     188        """ 
     189        The GMarker object may initialize on GEOS Points or a parameter 
     190        that may be instantiated into a GEOS point.  Keyword options map to 
     191        GMarkerOptions -- so far only the title option is supported. 
     192 
     193        Keyword Options: 
     194         title:  
     195           Title option for GMarker, will be displayed as a tooltip. 
     196        """ 
     197        # If a GEOS geometry isn't passed in, try to construct one. 
     198        if isinstance(geom, basestring): geom = fromstr(geom) 
     199        if isinstance(geom, (tuple, list)): geom = Point(geom) 
     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        # TODO: Add support for more GMarkerOptions 
     207        self.title = title 
     208        super(GMarker, self).__init__() 
     209 
     210    def latlng_from_coords(self, coords): 
     211        return 'new GLatLng(%s,%s)' %(coords[1], coords[0]) 
     212     
     213    def options(self): 
     214        result = [] 
     215        if self.title: result.append('title: "%s"' % self.title)  
     216        return '{%s}' % ','.join(result) 
     217 
     218    @property 
     219    def js_params(self): 
     220        return '%s, %s' % (self.latlng, self.options())