Skip to content

Commit 1f0a767

Browse files
committed
Put the javascript files into the Media class.
In this way is avoid the multiple inclusion of the Google API javascript file.
1 parent 5f9eed9 commit 1f0a767

File tree

4 files changed

+35
-3
lines changed

4 files changed

+35
-3
lines changed

README.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ Installation
1717
pip install django-easy-maps
1818

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

2224
Settings
2325
========

easy_maps/static/js/easy_maps.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// will contain the boundary of the map.
2+
var g_lat_long_bound = new google.maps.LatLngBounds();
3+
4+
function easy_map_add_listener(id, marker) {
5+
// update the coordinate on marker dragging
6+
google.maps.event.addListener(marker, 'dragend', function(evt) {
7+
var ll = marker.getPosition();
8+
// FIXME: fix id names
9+
document.getElementById(id + 'latitude').value = ll.lat();
10+
document.getElementById(id + 'longitude').value = ll.lng();
11+
});
12+
}
13+
14+
function easy_maps_add_marker(map, marker) {
15+
var latlng = new google.maps.LatLng(marker.latitude, marker.longitude);
16+
var marker = new google.maps.Marker({
17+
position: latlng,
18+
map: map,
19+
draggable: true,
20+
title: marker.address
21+
});
22+
23+
// add marker's coordinate to the boundary
24+
g_lat_long_bound.extend(latlng);
25+
26+
return marker;
27+
}

easy_maps/templates/easy_maps/map.html

-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
{% with longitude|stringformat:"f" as long %}
44

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

108
{% block html %}

easy_maps/widgets.py

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
from django.template import Template, Context
33

44
class AddressWithMapWidget(TextInput):
5+
class Media:
6+
js = (
7+
'https://maps.google.com/maps/api/js?sensor=false',
8+
'js/easy_maps.js',
9+
)
510
def render(self, name, value, attrs=None):
611
# retrieve the field's id otherwise it's not possible
712
# to use correctly the JS

0 commit comments

Comments
 (0)