Ticket #7619: gis-googlemaps.diff
File gis-googlemaps.diff, 10.3 KB (added by , 16 years ago) |
---|
-
django/contrib/gis/maps/google/gmap.py
4 4 from django.utils.safestring import mark_safe 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) 10 10 # TODO: Internationalize for Japan, UK, etc. … … 20 20 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=[], points=[], 24 24 template='gis/google/js/google-map.js', 25 25 extra_context={}): 26 26 … … 56 56 self.kml_urls = kml_urls 57 57 58 58 # 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)) 60 66 if polygons: 61 67 for poly in polygons: 62 68 if isinstance(poly, GPolygon): … … 70 76 else: 71 77 self.polylines.append(GPolyline(pline)) 72 78 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 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.points: 79 86 if center is None or zoom is None: 80 87 self.calc_zoom = True 81 88 … … 95 102 'zoom' : self.zoom, 96 103 'polygons' : self.polygons, 97 104 'polylines' : self.polylines, 105 'points' : self.points, 98 106 } 99 107 params.update(extra_context) 100 108 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 1 from django.contrib.gis.geos import LineString, LinearRing, Polygon, Point 2 2 from django.utils.safestring import mark_safe 3 3 4 class 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 4 55 class GOverlayBase(object): 56 def __init__(self): 57 self.events = [] 58 5 59 def latlng_from_coords(self, coords): 6 60 return '[%s]' % ','.join(['new GLatLng(%s,%s)' % (y, x) for x, y in coords]) 7 61 62 def add_event(self, event): 63 """Attaches a GEvent to the overlay object.""" 64 65 self.events.append(event) 66 8 67 def __unicode__(self): 9 68 "The string representation is the JavaScript API call." 10 69 return mark_safe('%s(%s)' % (self.__class__.__name__, self.js_params)) … … 40 99 The opacity of the polygon fill. Defaults to 0.4. 41 100 """ 42 101 102 GOverlayBase.__init__(self) 103 43 104 # TODO: Take other types of geometries. 44 105 if not isinstance(poly, Polygon): 45 106 raise TypeError('GPolygon may only initialize on GEOS Polygons.') … … 85 146 opacity: 86 147 The opacity of the polyline, between 0 and 1. Defaults to 1. 87 148 """ 149 GOverlayBase.__init__(self) 88 150 if isinstance(geom, (LineString, LinearRing)): 89 151 self.latlngs = self.latlng_from_coords(geom.coords) 90 152 elif isinstance(geom, Polygon): … … 99 161 @property 100 162 def js_params(self): 101 163 return '%s, "%s", %s, %s' % (self.latlngs, self.color, self.weight, self.opacity) 164 165 166 class 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
8 8 {% if calc_zoom %}var bounds = new GLatLngBounds(); var tmp_bounds = new GLatLngBounds();{% else %}map.setCenter(new GLatLng({{ center.1 }}, {{ center.0 }}), {{ zoom }});{% endif %} 9 9 {% for kml_url in kml_urls %}var kml{{ forloop.counter }} = new GGeoXml("{{ kml_url }}"); 10 10 map.addOverlay(kml{{ forloop.counter }});{% endfor %} 11 11 12 {% 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 %} 13 17 tmp_bounds = poly{{ forloop.counter }}.getBounds(); bounds.extend(tmp_bounds.getSouthWest()); bounds.extend(tmp_bounds.getNorthEast());{% endif %}{% endfor %} 18 14 19 {% 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 %} 16 24 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 17 35 {% if calc_zoom %}map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));{% endif %} 18 36 {% block load_extra %}{% endblock %} 19 37 }else {