Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing layerscontrol variable (or incomplete documentation) #341

Open
thibault opened this issue Sep 1, 2021 · 2 comments
Open

Missing layerscontrol variable (or incomplete documentation) #341

thibault opened this issue Sep 1, 2021 · 2 comments

Comments

@thibault
Copy link
Contributor

thibault commented Sep 1, 2021

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
Copy link

JackAtOmenApps commented Sep 4, 2021

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

@Gagaro
Copy link
Member

Gagaro commented Sep 7, 2021

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);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants