Skip to content

Commit

Permalink
Put the javascript files into the Media class.
Browse files Browse the repository at this point in the history
In this way is avoid the multiple inclusion of the Google API
javascript file.
  • Loading branch information
gipi committed Jan 3, 2013
1 parent 5f9eed9 commit 1f0a767
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
4 changes: 3 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ Installation
pip install django-easy-maps

Then add 'easy_maps' to INSTALLED_APPS and run ``./manage.py syncdb``
(or ``./manage.py migrate easy_maps`` if South is in use)
(or ``./manage.py migrate easy_maps`` if South is in use). Since there are
some media files needed to be used, you have to collect the static files
distributed with this application (using ``./manage collectstatic``).

Settings
========
Expand Down
27 changes: 27 additions & 0 deletions easy_maps/static/js/easy_maps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// will contain the boundary of the map.
var g_lat_long_bound = new google.maps.LatLngBounds();

function easy_map_add_listener(id, marker) {
// update the coordinate on marker dragging
google.maps.event.addListener(marker, 'dragend', function(evt) {
var ll = marker.getPosition();
// FIXME: fix id names
document.getElementById(id + 'latitude').value = ll.lat();
document.getElementById(id + 'longitude').value = ll.lng();
});
}

function easy_maps_add_marker(map, marker) {
var latlng = new google.maps.LatLng(marker.latitude, marker.longitude);
var marker = new google.maps.Marker({
position: latlng,
map: map,
draggable: true,
title: marker.address
});

// add marker's coordinate to the boundary
g_lat_long_bound.extend(latlng);

return marker;
}
2 changes: 0 additions & 2 deletions easy_maps/templates/easy_maps/map.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
{% with longitude|stringformat:"f" as long %}

{% block api_js %}
<!-- Google Maps API javascript -->
<script type="text/javascript" src="https://maps.google.com/maps/api/js?sensor=false"></script>
{% endblock %}

{% block html %}
Expand Down
5 changes: 5 additions & 0 deletions easy_maps/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
from django.template import Template, Context

class AddressWithMapWidget(TextInput):
class Media:
js = (
'https://maps.google.com/maps/api/js?sensor=false',
'js/easy_maps.js',
)
def render(self, name, value, attrs=None):
# retrieve the field's id otherwise it's not possible
# to use correctly the JS
Expand Down

0 comments on commit 1f0a767

Please sign in to comment.