Changeset 7841 for django/branches/gis/django/contrib/gis/maps
- Timestamp:
- 07/05/08 17:10:33 (5 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/gis/django/contrib/gis/maps/google/gmap.py
r7400 r7841 5 5 6 6 class GoogleMapException(Exception): pass 7 from django.contrib.gis.maps.google.overlays import GPolygon, GPolyline 7 from django.contrib.gis.maps.google.overlays import GPolygon, GPolyline, GMarker 8 8 9 9 # The default Google Maps URL (for the API javascript) … … 21 21 def __init__(self, key=None, api_url=None, version=None, 22 22 center=None, zoom=None, dom_id='map', load_func='gmap_load', 23 kml_urls=[], polygons=[], polylines=[], 23 kml_urls=[], polygons=[], polylines=[], markers=[], 24 24 template='gis/google/js/google-map.js', 25 25 extra_context={}): … … 56 56 self.kml_urls = kml_urls 57 57 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)) 60 66 if polygons: 61 67 for poly in polygons: … … 71 77 self.polylines.append(GPolyline(pline)) 72 78 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 74 81 # calculated via the Google Maps API. If both a zoom level and a 75 82 # center coordinate are provided with polygons/polylines, no automatic 76 83 # determination will occur. 77 84 self.calc_zoom = False 78 if self.polygons or self.polylines :85 if self.polygons or self.polylines or self.markers: 79 86 if center is None or zoom is None: 80 87 self.calc_zoom = True … … 96 103 'polygons' : self.polygons, 97 104 'polylines' : self.polylines, 105 'markers' : self.markers, 98 106 } 99 107 params.update(extra_context) django/branches/gis/django/contrib/gis/maps/google/__init__.py
r7400 r7841 58 58 """ 59 59 from django.contrib.gis.maps.google.gmap import GoogleMap 60 from django.contrib.gis.maps.google.overlays import G Polygon, GPolyline60 from django.contrib.gis.maps.google.overlays import GEvent, GMarker, GPolygon, GPolyline 61 61 from 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, Polygon2 1 from django.utils.safestring import mark_safe 2 from django.contrib.gis.geos import fromstr, Point, LineString, LinearRing, Polygon 3 4 class 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)) 3 51 4 52 class GOverlayBase(object): 53 def __init__(self): 54 self.events = [] 55 5 56 def latlng_from_coords(self, coords): 57 "Generates a JavaScript array of GLatLng objects for the given coordinates." 6 58 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) 7 63 8 64 def __unicode__(self): … … 20 76 fill_color='#0000ff', fill_opacity=0.4): 21 77 """ 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. 24 81 25 82 Keyword Options: … … 40 97 The opacity of the polygon fill. Defaults to 0.4. 41 98 """ 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) 44 101 if not isinstance(poly, Polygon): 45 102 raise TypeError('GPolygon may only initialize on GEOS Polygons.') … … 58 115 # Fill settings. 59 116 self.fill_color, self.fill_opacity = fill_color, fill_opacity 60 117 118 super(GPolygon, self).__init__() 119 61 120 @property 62 121 def js_params(self): … … 72 131 def __init__(self, geom, color='#0000ff', weight=2, opacity=1): 73 132 """ 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. 76 136 77 137 Keyword Options: … … 86 146 The opacity of the polyline, between 0 and 1. Defaults to 1. 87 147 """ 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. 88 152 if isinstance(geom, (LineString, LinearRing)): 89 153 self.latlngs = self.latlng_from_coords(geom.coords) … … 96 160 self.envelope = geom.envelope 97 161 self.color, self.weight, self.opacity = color, weight, opacity 162 super(GPolyline, self).__init__() 98 163 99 164 @property 100 165 def js_params(self): 101 166 return '%s, "%s", %s, %s' % (self.latlngs, self.color, self.weight, self.opacity) 167 168 class 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())
