app/django/contrib/gis/maps/google/gmap.py
changeset 323 ff1a9aa48cfd
equal deleted inserted replaced
322:6641e941ef1e 323:ff1a9aa48cfd
       
     1 from django.conf import settings
       
     2 from django.contrib.gis import geos
       
     3 from django.template.loader import render_to_string
       
     4 from django.utils.safestring import mark_safe
       
     5 
       
     6 class GoogleMapException(Exception): pass
       
     7 from django.contrib.gis.maps.google.overlays import GPolygon, GPolyline, GMarker
       
     8 
       
     9 # The default Google Maps URL (for the API javascript)
       
    10 # TODO: Internationalize for Japan, UK, etc.
       
    11 GOOGLE_MAPS_URL='http://maps.google.com/maps?file=api&v=%s&key='
       
    12 
       
    13 class GoogleMap(object):
       
    14     "A class for generating Google Maps JavaScript."
       
    15 
       
    16     # String constants
       
    17     onunload = mark_safe('onunload="GUnload()"') # Cleans up after Google Maps
       
    18     vml_css  = mark_safe('v\:* {behavior:url(#default#VML);}') # CSS for IE VML
       
    19     xmlns    = mark_safe('xmlns:v="urn:schemas-microsoft-com:vml"') # XML Namespace (for IE VML).
       
    20 
       
    21     def __init__(self, key=None, api_url=None, version=None, 
       
    22                  center=None, zoom=None, dom_id='map', load_func='gmap_load', 
       
    23                  kml_urls=[], polygons=[], polylines=[], markers=[],
       
    24                  template='gis/google/js/google-map.js',
       
    25                  extra_context={}):
       
    26 
       
    27         # The Google Maps API Key defined in the settings will be used
       
    28         #  if not passed in as a parameter.  The use of an API key is
       
    29         #  _required_.
       
    30         if not key:
       
    31             try:
       
    32                 self.key = settings.GOOGLE_MAPS_API_KEY
       
    33             except AttributeError:
       
    34                 raise GoogleMapException('Google Maps API Key not found (try adding GOOGLE_MAPS_API_KEY to your settings).')
       
    35         else:
       
    36             self.key = key
       
    37         
       
    38         # Getting the Google Maps API version, defaults to using the latest ("2.x"),
       
    39         #  this is not necessarily the most stable.
       
    40         if not version:
       
    41             self.version = getattr(settings, 'GOOGLE_MAPS_API_VERSION', '2.x')
       
    42         else:
       
    43             self.version = version
       
    44 
       
    45         # Can specify the API URL in the `api_url` keyword.
       
    46         if not api_url:
       
    47             self.api_url = mark_safe(getattr(settings, 'GOOGLE_MAPS_URL', GOOGLE_MAPS_URL) % self.version)
       
    48         else:
       
    49             self.api_url = api_url
       
    50 
       
    51         # Setting the DOM id of the map, the load function, the JavaScript
       
    52         # template, and the KML URLs array.
       
    53         self.dom_id = dom_id
       
    54         self.load_func = load_func
       
    55         self.template = template
       
    56         self.kml_urls = kml_urls
       
    57         
       
    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))
       
    66         if polygons:
       
    67             for poly in polygons:
       
    68                 if isinstance(poly, GPolygon): 
       
    69                     self.polygons.append(poly)
       
    70                 else:
       
    71                     self.polygons.append(GPolygon(poly))
       
    72         if polylines:
       
    73             for pline in polylines:
       
    74                 if isinstance(pline, GPolyline):
       
    75                     self.polylines.append(pline)
       
    76                 else:
       
    77                     self.polylines.append(GPolyline(pline))
       
    78        
       
    79         # If GMarker, GPolygons, and/or GPolylines 
       
    80         # are used the zoom will be automatically
       
    81         # calculated via the Google Maps API.  If both a zoom level and a
       
    82         # center coordinate are provided with polygons/polylines, no automatic
       
    83         # determination will occur.
       
    84         self.calc_zoom = False
       
    85         if self.polygons or self.polylines  or self.markers:
       
    86             if center is None or zoom is None:
       
    87                 self.calc_zoom = True
       
    88     
       
    89         # Defaults for the zoom level and center coordinates if the zoom
       
    90         # is not automatically calculated.
       
    91         if zoom is None: zoom = 4
       
    92         self.zoom = zoom
       
    93         if center is None: center = (0, 0)
       
    94         self.center = center
       
    95 
       
    96         # Setting the parameters for the javascript template.
       
    97         params = {'calc_zoom' : self.calc_zoom,
       
    98                   'center' : self.center,
       
    99                   'dom_id' : self.dom_id,
       
   100                   'kml_urls' : self.kml_urls,
       
   101                   'load_func' : self.load_func,
       
   102                   'zoom' : self.zoom,
       
   103                   'polygons' : self.polygons,
       
   104                   'polylines' : self.polylines,
       
   105                   'markers' : self.markers,
       
   106                   }
       
   107         params.update(extra_context)
       
   108         self.js = render_to_string(self.template, params)
       
   109 
       
   110     @property
       
   111     def body(self):
       
   112         "Returns HTML body tag for loading and unloading Google Maps javascript."
       
   113         return mark_safe('<body %s %s>' % (self.onload, self.onunload))
       
   114 
       
   115     @property
       
   116     def onload(self):
       
   117         "Returns the `onload` HTML <body> attribute."
       
   118         return mark_safe('onload="%s()"' % self.load_func)
       
   119 
       
   120     @property
       
   121     def api_script(self):
       
   122         "Returns the <script> tag for the Google Maps API javascript."
       
   123         return mark_safe('<script src="%s%s" type="text/javascript"></script>' % (self.api_url, self.key))
       
   124 
       
   125     @property
       
   126     def scripts(self):
       
   127         "Returns all <script></script> tags required for Google Maps JavaScript."
       
   128         return mark_safe('%s\n  <script type="text/javascript">\n//<![CDATA[\n%s//]]>\n  </script>' % (self.api_script, self.js))
       
   129 
       
   130     @property
       
   131     def style(self):
       
   132         "Returns additional CSS styling needed for Google Maps on IE."
       
   133         return mark_safe('<style type="text/css">%s</style>' % self.vml_css)
       
   134 
       
   135     @property
       
   136     def xhtml(self):
       
   137         "Returns XHTML information needed for IE VML overlays."
       
   138         return mark_safe('<html xmlns="http://www.w3.org/1999/xhtml" %s>' % self.xmlns)