makinacorpus / django-leaflet

Use Leaflet in your Django projects
GNU Lesser General Public License v3.0
713 stars 282 forks source link

Missing `layerscontrol` variable (or incomplete documentation) #341

Open thibault opened 3 years ago

thibault commented 3 years ago

Hi django-leaflet team,

I just installed django-leaflet and ran into an issue with a missing variable.

Context

I want to display a map with default values, and add an overlay layer. I followed the doc about adding overlays with js.

My code is pretty much exactly copy-pasted from the docs' excerpts.

{% block content %}
{% leaflet_map 'cadastre_map' callback="window.map_init" %}
{% endblock %}

{% block head %}
{% leaflet_css %}
{% leaflet_js %}
<script>
  function map_init(map, options) {
    var lc = map.layerscontrol;

    // An example from the Atlas of Living Australia https://www.ala.org.au/
    lc.addOverlay(
      L.tileLayer.wms(
        'https://spatial-beta.ala.org.au/geoserver/ALA/wms', {
          layers: 'ALA:aus2',
          format: 'image/png',
          transparent: true
        }),
      'Australia'
    );
  }
</script>
{% endblock %}

In my settings:

LEAFLET_CONFIG = {
    "DEFAULT_CENTER": (47, 1.7),
    "DEFAULT_ZOOM": 6,
}

Expected result

The overlay should display properly, according to the documentation.

Actual result

The console throws this error: Uncaught TypeError: lc is undefined

It seems to me that, according to this line or this one, the line that defines the layersControl var is never reached if no layers or overlays are defined in django settings.

I'm not sure if this is a bug with django-leaflet or is something is missing from the documentation?

Thanks

JackAtOmenApps commented 3 years ago

Edit: Had similar problem, but further troubleshooting showed it was an issue with differences in my settings for dev vs. prod.

Gagaro commented 3 years ago

I'd say it's from the documentation. There's no point in having a control if there is no layer. Maybe we should add that we need to create our own control in that case: var lc = L.control.layers().addTo(map);