Skip to content

Commit

Permalink
[#474] Testing new maps
Browse files Browse the repository at this point in the history
  • Loading branch information
KasperBrandt committed Apr 4, 2014
1 parent 682482c commit 8ca2399
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 12 deletions.
8 changes: 7 additions & 1 deletion akvo/rsr/templatetags/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from django.conf import settings
from django.http import Http404

from akvo.rsr.models import Project, Organisation
from akvo.rsr.models import Project, Organisation, ProjectLocation

register = template.Library()

Expand All @@ -38,6 +38,11 @@ def map(resource, width, height, type="dynamic", marker_icon=""):

map_id = 'akvo_map_%s' % os.urandom(8).encode('hex')

locations = []
locations_obj = ProjectLocation.objects.filter(location_target=resource.pk)
for location in locations_obj:
locations.append([location.latitude, location.longitude])

template_context = {
'map_id': map_id,
'type': type,
Expand All @@ -46,6 +51,7 @@ def map(resource, width, height, type="dynamic", marker_icon=""):
'host': HOST,
'MEDIA_URL': getattr(settings, 'MEDIA_URL', '/media/'),
'marker_icon': marker_icon,
'locations': locations
}
#extend this to support more models that have a location
supported_models = (Project, Organisation)
Expand Down
47 changes: 38 additions & 9 deletions akvo/templates/inclusion_tags/map.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,40 @@
<div class="akvo_map" id="{{map_id}}" style="width:{{width}}px;height:{{height}}px;"></div>
<div class= "akvo_map" id="{{map_id}}" style="width:{{width}}px;height:{{height}}px;"></div>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
{{map_id}} = {
"type": "{{type}}",
"object_id": "{{object_id}}",
"resource": "{{resource}}",
"marker_icon": "{{marker_icon}}",
"media_url": "{{MEDIA_URL}}",
"host": "{{host}}"
}
var googleMap = {
canvas: document.getElementById('{{map_id}}'),
options: {
mapTypeId: google.maps.MapTypeId.ROADMAP,
scrollwheel: false,
streetViewControl: false
},
locations: {{ locations|safe }},
load: function() {
var map = new google.maps.Map(this.canvas, this.options);
var bounds = new google.maps.LatLngBounds();

var i;

for (i = 0; i < this.locations.length; i++) {
marker = new google.maps.Marker({
icon: '/media/core/img/blueMarker.png',
position: new google.maps.LatLng(this.locations[i][0], this.locations[i][1]),
map: map,
clickable: false
});

bounds.extend(marker.position);

}

map.fitBounds(bounds);
map.panToBounds(bounds);

if (map.getZoom() > 8){
map.setZoom(8)
}

}
};
window.onload = function (){googleMap.load()};
</script>
4 changes: 2 additions & 2 deletions akvo/templates/rsr/project/project_main.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% extends "rsr/project/project_base.html" %}
{% load webdesign addparam humanize i18n rsr_filters rsr_tags thumbnail counter_tags google_maps %}
{% load webdesign addparam humanize i18n rsr_filters rsr_tags thumbnail counter_tags map %}
{% load url from future %}

{% block head %}
Expand Down Expand Up @@ -59,7 +59,7 @@ <h2 class="black">{% trans 'Location' %}</h2>
</p>
{# Google Map #}
{% if p.primary_location %}
{% google_map project 260 180 8 %}
{% map project 260 180 "small" %}
{% else %}
<p style="color: red;">{% trans 'No map available' %}</p>
{% endif %}
Expand Down

0 comments on commit 8ca2399

Please sign in to comment.