Commits

everett_toews committed a2f6ccb

Added feature to allow latitude and longitude instead of an addresss. Backwards compatible so no changes should need to be made to existing code.

  • Participants
  • Parent commits dcffaf6
  • Branches latlong

Comments (0)

Files changed (1)

File easy_maps/templatetags/easy_maps_tags.py

 from easy_maps.models import Address
 register = template.Library()
 
+LATITUDE_MIN = -90
+LATITUDE_MAX = 90
+LONGITUDE_MIN = -180
+LONGITUDE_MAX = 180
+
 @register.tag
 def easy_map(parser, token):
     """
     The syntax:
-        {% easy_map <address> [<width> <height>] [<zoom>] [using <template_name>] %}
+        {% easy_map <address or latitude longitude> [<width> <height>] [<zoom>] [using <template_name>] %}
     """
     width, height, zoom, template_name = None, None, None, None
     params = token.split_contents()
         params = params[:-2]
 
     if len(params) < 2:
-        raise template.TemplateSyntaxError('easy_map tag requires address argument')
+        raise template.TemplateSyntaxError('easy_map tag requires address or latitude longitude argument')
 
     address = params[1]
 
         width, height, zoom = params[2], params[3], params[4]
     elif len(params) == 3 or len(params) > 5:
         raise template.TemplateSyntaxError('easy_map tag has the following syntax: '
-                   '{% easy_map <address> <width> <height> [zoom] [using <template_name>] %}')
+                   '{% easy_map <address or latitude longitude> <width> <height> [zoom] [using <template_name>] %}')
     return EasyMapNode(address, width, height, zoom, template_name)
 
 class EasyMapNode(template.Node):
         self.zoom = zoom or 16
         self.template_name = template.Variable(template_name or '"easy_maps/map.html"')
 
+    def get_address(self, location):
+        location_split = location.split()
+
+        if len(location_split) == 2:
+            try:
+                latitude = float(location_split[0])
+                longitude = float(location_split[1])
+
+                if LATITUDE_MIN <= latitude <= LATITUDE_MAX and \
+                   LONGITUDE_MIN <= longitude <= LONGITUDE_MAX:
+                    address, _ = Address.objects.get_or_create(latitude=latitude, longitude=longitude)
+                    return address 
+            except ValueError as e:
+                pass
+
+        address, _ = Address.objects.get_or_create(address=location or '')
+        return address
+
     def render(self, context):
         try:
             address = self.address.resolve(context)
             template_name = self.template_name.resolve(context)
 
-            map, _ = Address.objects.get_or_create(address=address or '')
             context.update({
-                'map': map,
+                'map': self.get_address(address),
                 'width': self.width,
                 'height': self.height,
                 'zoom': self.zoom,