Django-geoportail provides a great template library that you can use to display read-only maps in your templates (with or without drawn features). There are two ways to enable this library:
On a project basis: if you need django-geoportail‘s tags in almost every template of your project, you can add in your project’s __init__.py:
from django import template
template.add_to_builtins('geoportal.templatetags.geoportal_tags')
On a per-template basis: simply add this line on the top of the template where you need geoportal’s template tags:
{% load geoportal_tags %}
Once you’ve loaded the template library, you need to load the geoportal javascript library on each page that displays a map. This is achieved by adding the following in the head section of your template:
{% geoportal_js %}
The above code will add a <script> tag that calls the Geoportal library. This has to be done before the geoportal_map tag is called: the javascript library has to be loaded synchronously.
Syntax:
{% geoportal_map field [option1=value, option2=other_value] [as var_name] %}
geoportal_tags provides a single template tag: the {% geoportal_map %} tag. It takes a required argument and several options.
The required argument is a geographic field. It can be a point, a linestring, a polygon or even a set of polygons or any type of geographic field. The simplest way to display a geographic field is (assuming you have a model_instance variable in your template context):
{% geoportal_map model_instance.geo_field %}
When this template tag is called, django-geoportail renders all the javascript code that is needed to display the field, and renders a map with the default options. With the Polygon I draw in the Example section, the map would look like this:
This is a map with the standard dimensions (600x400 pixels), the default feature color (#ee9900) and the default feature opacity (0.4).
When a map is rendered on a template, a random javascript variable is generated and this variable stores the map’s information. If you need to do some further customization, you can get this variable in your template context:
{% geoportal_map geo_field as var_name %}
This will still render the map and you will be able to manipulate it with some javascript code:
<script type="text/javascript">
{{ var_name }}.viewer.setSize(...);
{{ var_name }}.viewer.map.setCenter(...);
</script>
Here is the detail of what {{ var_name }} contains:
{{ var_name }} also contains several function definitions and some internal variables. Feel free to read the generated code and see what’s interesting.
The as var_name argument should always be the last argument. If you need any option (see below), you should always specify them before.
We’ve seen a standard map but it has to be customized: the feature color is too close to the color of Italy on the map. This is one of the things we can change.
Several options are available. The syntax for all options is the following: each options is a pair key=value, and two pairs must be separated by a comma. Some whitespace can be added since it often improves readability.
Here is an example of rendering a map with option1 set to 1 and option2 set to ff0000:
{% geoportal_map model_instance.geo_field option1=1, option2=ff0000 %}
Option values can be static strings or template variables. If some_value is available in the template context, calling
{% geoportal_map model_instance.geo_field option1=some_value %}
will resolve some_value from the template context. This can be very useful when some parameters have to be dynamic.
Note
Boolean options
Boolean values should be 0 (false) or 1 (true), nothing else.
Here is a full list of the available options and their default values.
Let’s finish with an example:
{% geoportal_map model_instance.point color=ff0000, opacity=0.7, zoom=14, width=300, height=300 %}
Here we generate a squared (300x300 pixels) map of a point. The point will be red with a rather high opacity. The zoom level is forced to 14 in case it is overriden in the settings.
The result is, as we can expect (oh, and the point has the coordinates of the Mont Blanc):
In this section, we assume that you have displayed a map on your page using the geoportal_map template tag, and you’ve added it to your template context:
{% geoportal_map my_field as var_name %}
Now, you can render KML and GPX filed with the corresponding template tags.
Syntax:
{% geoportal_kml map_var kml_url [option1=value1, option2=value2 ...] %}
The two required arguments are:
Options are comma-separated, using the key=value syntax described in the geoportal_map section. The values can be raw strings or template variables. The available options are:
Note
On style extraction
If you don’t specify any options, the feature will be styled according to the KML file you’re loading. If you specify any styling option manually, no style will be extracted from the feature. That means you can’t specify extract=1 and width=4 at the same time for example. Setting any other option will autmatically set extract to 0 and the remaning ones to to their default values.
Syntax:
{% geoportal_gpx map_var gpx_url [option1=value1, option2=value2 ...] %}
Like with the geoportal_kml tag, you need to specify the map variable and the URL of the GPX file.
The available options are:
Note
On focus
If you add several KML/GPX features to the map, make sure the focus parameter is set to 1 only once. If not, the map rendering can become unpredictable.