I wanted to be able to show locations by latitude and longitude with data from Django 1.8 website on a Google Map using the Google Maps API.
I used the accepted answer from a stackoverflow question to create a Google Map with multiple markers that had the store name with a link that would open that store’s details when clicked.
I did the following:
- copied the stackoverflow solution javascript and html code into new Django template called fountain_map.html
- created new Django view called fountain_map for that template
- create new urls.py line to route the fountain_map url for new view/template
The stackoverflow answer used Google Maps javascript that had a javascript array like this:
var locations = [
[‘Bondi Beach’, -33.890542, 151.274856, 4],
[‘Coogee Beach’, -33.923036, 151.259052, 5],
[‘Cronulla Beach’, -34.028249, 151.157507, 3],
[‘Manly Beach’, -33.80010128657071, 151.28747820854187, 2],
[‘Maroubra Beach’, -33.950198, 151.259302, 1]
];
However while the example has this hard coded list of locations I wanted a dynamic list populated by queryset records from the new view.
So I created a queryset in the view that retrieved the location records:
map_points = Fountains.objects.filter(lat__isnull=False)
Note that I filtered to only retrieve records that had a lat value so I wasn’t sending records that couldn’t be mapped.
Since the queryset object is not immediately readable by the javascript as the location variable, it needed to be transformed into a format acceptable for the javascript.
There are a couple of options:
- Use Django’s serialization to turn it into JSON
- Loop through queryset object and manually build the array in correct format, this could be done in the view or in the template
I choose to do this transformation in the template. Django’s serialization has lots of documentation and lots of SO question and answer but seemed easier to do this in template for now.
So in the template i simply looped through the map_point queryset object to create the array that the var locations required.
The javascript required the square brackets as shown in example above along with quotes around the location name.
Note that the Stack Overflow answer also has a digit as the fourth item in the record but I excluded that in mine. Not sure what it was but user obviously wanted to show it in marker label or something like that.
Anyways my template loop looked like this:
var locations = [
{% for point in map_points %}
{% if point.lat = None %}
{% else %}
{{ point.name }}’, {{ point.lat }}, {{ point.lon }}],
{% endif %}
{% endfor%}
]
You can see that I retrieved the following values for the locations array from the queryset results:
- name (fountain name to show on marker label popup)
- id (so that it could be used to create marker link to the Django view for that store)
- lat
- lon
That was all I needed to do and gave me a Google Map showing each fountain’s location with a Google red pin marker. When user clicked on marker, the fountain name would show that had link to that fountain’s detail page.
