Ticket #14186: geodjango-gdirections.patch

File geodjango-gdirections.patch, 5.1 KB (added by Miguel Araujo, 14 years ago)
  • django/contrib/gis/templates/gis/google/google-map.js

     
    2222    {{ js_module }}.{{ dom_id }}.addOverlay({{ js_module }}.{{ dom_id }}_polyline{{ forloop.counter }});
    2323    {% for event in polyline.events %}GEvent.addListener({{ js_module }}.{{ dom_id }}_polyline{{ forloop.parentloop.counter }}, {{ event }}); {% endfor %}
    2424    {% if calc_zoom %}tmp_bounds = {{ js_module }}.{{ dom_id }}_polyline{{ forloop.counter }}.getBounds(); bounds.extend(tmp_bounds.getSouthWest()); bounds.extend(tmp_bounds.getNorthEast());{% endif %}{% endfor %}
     25    {% if directions %}{{ js_module }}.{{ dom_id }}_directions = new GDirections({{ js_module }}.{{ dom_id }});
     26    {{ js_module }}.{{ dom_id }}_directions.loadFromWaypoints({{ directions }});{% endif %}
    2527    {% for marker in markers %}{{ js_module }}.{{ dom_id }}_marker{{ forloop.counter }} = new {{ marker }};
    2628    {{ js_module }}.{{ dom_id }}.addOverlay({{ js_module }}.{{ dom_id }}_marker{{ forloop.counter }});
    2729    {% for event in marker.events %}GEvent.addListener({{ js_module }}.{{ dom_id }}_marker{{ forloop.parentloop.counter }}, {{ event }}); {% endfor %}
  • 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, GMarker, GIcon
     7from django.contrib.gis.maps.google.overlays import GPolygon, GPolyline, GMarker, GIcon, GDirections
    88
    99# The default Google Maps URL (for the API javascript)
    1010# TODO: Internationalize for Japan, UK, etc.
     
    2121    def __init__(self, key=None, api_url=None, version=None,
    2222                 center=None, zoom=None, dom_id='map',
    2323                 kml_urls=[], polylines=None, polygons=None, markers=None,
     24                 directions=None,
    2425                 template='gis/google/google-map.js',
    2526                 js_module='geodjango',
    2627                 extra_context={}):
     
    5657        self.js_module = js_module
    5758        self.template = template
    5859        self.kml_urls = kml_urls
     60        # Does the user want to draw google directions on the map?
     61        self.directions = directions;
    5962
    6063        # Does the user want any GMarker, GPolygon, and/or GPolyline overlays?
    6164        overlay_info = [[GMarker, markers, 'markers'],
     
    101104                  'polylines' : self.polylines,
    102105                  'icons': self.icons,
    103106                  'markers' : self.markers,
     107                  'directions' : self.directions,
    104108                  }
    105109        params.update(self.extra_context)
    106110        return render_to_string(self.template, params)
  • django/contrib/gis/maps/google/overlays.py

     
    299299    @property
    300300    def js_params(self):
    301301        return '%s, %s' % (self.latlng, self.options())
     302
     303class GDirections():
     304    """
     305    A Python wrapper for the Google GDirections object.  For more information
     306    please see the Google Maps API Reference:
     307     http://code.google.com/apis/maps/documentation/reference.html#GDirections
     308
     309    Example:
     310
     311      from django.shortcuts import render_to_response
     312      from django.contrib.gis.maps.google.overlays import GDirections
     313
     314      def sample_request(request):
     315          route = GDirections(LineString(POINT(40.44 -3.77), POINT(42.33 -3.66))
     316          return render_to_response('mytemplate.html',
     317                 {'google' : GoogleMap(directions=route)})
     318    """
     319
     320    def __init__(self, geom):
     321        """
     322        The GDirections object may be initialized on GEOS LineString, LinearRing,
     323        and Polygon objects (internal rings not supported) or a parameter that
     324        may instantiated into one of the above geometries.
     325        """
     326        # If a GEOS geometry isn't passed in, try to contsruct one.
     327        if isinstance(geom, basestring): geom = fromstr(geom)
     328        if isinstance(geom, (tuple, list)): geom = Polygon(geom)
     329        # Generating the lat/lng coordinate pairs.
     330        if isinstance(geom, (LineString, LinearRing)):
     331            self.latlngs = self.waypoints(geom.coords)
     332        elif isinstance(geom, Polygon):
     333            self.latlngs = self.waypoints(geom.shell.coords)
     334        else:
     335            raise TypeError('GPolyline may only initialize on GEOS LineString, LinearRing, and/or Polygon geometries.')
     336
     337    def waypoints(self, coords):
     338        "Generates a JavaScript array of strings for the given coordinates."
     339        return '[%s]' % ','.join(['"%s,%s"' % (y, x) for x, y in coords])
     340
     341    def __unicode__(self):
     342        return self.latlngs
Back to Top