Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
fcurella committed Jul 10, 2012
0 parents commit ada084f
Show file tree
Hide file tree
Showing 20 changed files with 340 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,3 @@
*.pyc
.DS_Store

12 changes: 12 additions & 0 deletions AUTHORS
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,12 @@
django-google-maps is written and maintained by Flavio Curella and
various contributors:

Development Lead
~~~~~~~~~~~~~~~~

- Flavio Curella <flavio.curella@gmail.com>


Patches and Suggestions
~~~~~~~~~~~~~~~~~~~~~~~

19 changes: 19 additions & 0 deletions LICENSE
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (C) 2012 by Flavio Curella and individual contributors.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
8 changes: 8 additions & 0 deletions MANIFEST.in
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,8 @@
recursive-include tests *.py

include AUTHORS
include LICENSE
include README.rst

recursive-include templates/gmaps/templatetags/gmaps/ *.js
recursive-include templates/gmaps/templatetags/gmaps/ *.html
27 changes: 27 additions & 0 deletions README.rst
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,27 @@
Django Google Maps
======================================

This app provides template tags for easily creating Google Maps from GeoDjango models.

Installation
~~~~~~~~~~~~

1. Add ``GOOGLE_API_KEY`` to your settings.
2. Add ``gmaps`` to your ``INSTALLED_APPS``.


Usage
~~~~~

{% load gmaps %}
<div id="mymap"></div>

{% gmap_js %} {# prints out the <script> tag calling the Google Maps API #}

{% map "mymap" object.location %}
{% marker someobj.location %}
{# all tags accept optional parameters that will be passed to the js constructor #}
{% marker someobj.location ["title" "'Hello, World!'"] %}
{% polygon someobj.mpoly %}
{% polyline someobj.mpoly %}
{% endmap %}
1 change: 1 addition & 0 deletions gmaps/__init__.py
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = (0, 0, 1)
1 change: 1 addition & 0 deletions gmaps/models.py
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1 @@
# Intentionally left blank.
3 changes: 3 additions & 0 deletions gmaps/templates/gmaps/templatetags/gmaps/gmap_js.html
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,3 @@
<script type="text/javascript"
src="//maps.googleapis.com/maps/api/js?key={{ GMAPS_API_KEY }}&sensor={{ sensor|yesno:"true,false"}}">
</script>
5 changes: 5 additions & 0 deletions gmaps/templates/gmaps/templatetags/gmaps/kwargs.js
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,5 @@
{% if map_var %},
map: {{ map_var }}
{% endif %}
{% if kwargs %},{% endif %}
{% for k,v in kwargs.items %}{{k}}: {{v}}{% if not forloop.last%},{% endif %}{% endfor %}
5 changes: 5 additions & 0 deletions gmaps/templates/gmaps/templatetags/gmaps/load_handler.js
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,5 @@
<script type="text/javascript">
$().ready(function() {
{{ output }}
});
</script>
6 changes: 6 additions & 0 deletions gmaps/templates/gmaps/templatetags/gmaps/map.js
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,6 @@
var {{ map_var }} = new google.maps.Map(document.getElementById('{{ element_id }}'), {
center: new google.maps.LatLng({{ latitude }}, {{ longitude }}),
zoom: {{ zoom }},
mapTypeId: {{ map_type }}
{% include "quakeparser/templatetags/gmaps/kwargs.js" %}
});
4 changes: 4 additions & 0 deletions gmaps/templates/gmaps/templatetags/gmaps/marker.js
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,4 @@
new google.maps.Marker({
position: new google.maps.LatLng({{ latitude }}, {{ longitude }})
{% include "quakeparser/templatetags/gmaps/kwargs.js" %}
});
8 changes: 8 additions & 0 deletions gmaps/templates/gmaps/templatetags/gmaps/polygon.js
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,8 @@
new google.maps.Polygon({
paths: [
{% for point in coords %}
new google.maps.LatLng({{ point.1 }}, {{ point.0 }}){% if not forloop.last %},{% endif %}
{% endfor %}
]
{% include "quakeparser/templatetags/gmaps/kwargs.js" %}
});
8 changes: 8 additions & 0 deletions gmaps/templates/gmaps/templatetags/gmaps/polyline.js
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,8 @@
new google.maps.Polyline({
path: [
{% for point in coords %}
new google.maps.LatLng({{ point.1 }}, {{ point.0 }}){% if not forloop.last %},{% endif %}
{% endfor %}
]
{% include "quakeparser/templatetags/gmaps/kwargs.js" %}
});
Empty file added gmaps/templatetags/__init__.py
Empty file.
195 changes: 195 additions & 0 deletions gmaps/templatetags/gmaps.py
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,195 @@
"""
{% load gmaps %}
{% gmap_js [<sensor>] %}
{% map <element_id> <center_location> [zoom <zoom>] [map_type <type>] %}
{% marker <location> [title <title>] %}
{% marker <location> [title <title>] %}
{% polygon <mpoly> %}
{% endmap %}
"""

"""
function initialize() {
var map = new google.maps.Map(document.getElementById('map-canvas'), {
center: new google.maps.LatLng(37.4419, -122.1419),
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var marker = new google.maps.Marker({
position: new google.maps.LatLng(37.4440, -122.1419),
map: map,
title:"Hello World!"
});
var marker2 = new google.maps.Marker({
position: new google.maps.LatLng(37.4460, -122.1300),
map: map,
title:"Hello World!2"
});
var marker3 = new google.maps.Marker({
position: new google.maps.LatLng(37.4500, -122.1500),
map: map,
title:"Hello World!3"
});
var triangleCoords = [
new google.maps.LatLng(37.4440, -122.1419),
new google.maps.LatLng(37.4460, -122.1300),
new google.maps.LatLng(37.4500, -122.1500),
new google.maps.LatLng(37.4440, -122.1419)
];
var triangle = new google.maps.Polygon({
paths: triangleCoords,
strokeColor: "#FF0000",
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: "#FF0000",
fillOpacity: 0.35,
map: map
});
}
"""

from django import template
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured


register = template.Library()
TEMPLATE_ROOT = 'gmaps/templatetags/gmaps/'


def list_to_dict(x):
return dict(x[i:i + 2] for i in range(0, len(x), 2))


@register.inclusion_tag(TEMPLATE_ROOT + 'gmap_js.html')
def gmap_js(sensor=False):
api_key = getattr(settings, 'GOOGLE_API_KEY', None)
if api_key is None:
raise ImproperlyConfigured(u'You must define GOOGLE_API_KEY in your settings.')

return {
'sensor': sensor,
'GMAPS_API_KEY': api_key,
}


MAP_TYPES = {
'hybrid': 'google.maps.MapTypeId.HYBRID',
'roadmap': 'google.maps.MapTypeId.ROADMAP',
'satellite': 'google.maps.MapTypeId.SATELLITE',
'terrain': 'google.maps.MapTypeId.TERRAIN',
}


class MapNode(template.Node):
template_name = TEMPLATE_ROOT + 'map.js'
template_name_handler = TEMPLATE_ROOT + 'load_handler.js'

def __init__(self, nodelist, element_id, location, map_type='"roadmap"', zoom="13", **kwargs):
self.nodelist = nodelist
self.element_id = template.Variable(element_id)
self.location = template.Variable(location)
self.map_type = template.Variable(map_type)
self.zoom = template.Variable(zoom)

for k, v in kwargs.items():
kwargs[k] = template.Variable(v)
self.kwargs = kwargs

def render(self, context):
location = self.location.resolve(context)
lon = location.x
lat = location.y

element_id = self.element_id.resolve(context)
map_var = "%s_map" % element_id

for k, v in self.kwargs.items():
self.kwargs[k] = v.resolve(context)

ctx = {
'map_var': map_var,
'element_id': element_id,
'latitude': lat,
'longitude': lon,
'map_type': MAP_TYPES[self.map_type.resolve(context)],
'zoom': self.zoom.resolve(context),
'kwargs': self.kwargs
}
output = template.loader.render_to_string(self.template_name, ctx)

context['map_var'] = map_var
output += self.nodelist.render(context)

return template.loader.render_to_string(self.template_name_handler, {'output': output})


@register.tag
def map(parser, token):
bits = token.split_contents()[1:]
args = bits[:2]
kwargs = list_to_dict(bits[2:])
nodelist = parser.parse(('endmap',))
parser.delete_first_token()
return MapNode(nodelist, *args, **kwargs)


@register.inclusion_tag(TEMPLATE_ROOT + 'marker.js', takes_context=True)
def marker(context, location, *args):
"""
Outputs the necessary code to put a marker on the map.
{% marker <location> [options] %}
"""
kwargs = list_to_dict(args)

lon = location.x
lat = location.y

context.update({
'latitude': lat,
'longitude': lon,
'kwargs': kwargs
})

return context


@register.inclusion_tag(TEMPLATE_ROOT + 'polygon.js', takes_context=True)
def polygon(context, mpoly, *args):
"""
Given a GeoDjango mpoly fields, outputs the necessary code to generate a Polygon.
"""
kwargs = list_to_dict(args)
coords = mpoly.coords[0]

context.update({
'coords': coords,
'kwargs': kwargs
})
return context


@register.inclusion_tag(TEMPLATE_ROOT + 'polyline.js', takes_context=True)
def polyline(context, mpoly, *args):
"""
Given a GeoDjango mpoly fields, outputs the necessary code to generate a Polyline.
"""
kwargs = list_to_dict(args)
coords = mpoly.coords[0]

context.update({
'coords': coords,
'kwargs': kwargs
})

return context
1 change: 1 addition & 0 deletions gmaps/tests.py
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1 @@
# Wrtie tests here
Empty file added requirements.txt
Empty file.
34 changes: 34 additions & 0 deletions setup.py
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,34 @@
import os
from setuptools import setup, find_packages

from gmaps import __version__


def read(fname):
return open(os.path.join(os.path.dirname(__file__), fname)).read()

requirements = []

setup(
name = "django-google-maps",
version = ".".join(map(str, __version__)),
description = "",
long_description = read('README.rst'),
url = '',
license = 'MIT',
author = 'Flavio Curella',
author_email = 'flavio.curella@gmail.com',
packages = find_packages(exclude=['tests']),
classifiers = [
'Development Status :: 3 - Alpha',
'Environment :: Console',
'Intended Audience :: Developers',
'Intended Audience :: Information Technology',
'License :: OSI Approved :: MIT License',
'Operating System :: OS Independent',
'Programming Language :: Python',
'Framework :: Django',
],
install_requires = requirements,
tests_require = [],
)
Empty file added tests/__init__.py
Empty file.

0 comments on commit ada084f

Please sign in to comment.