Leaflet: t.addLayer error

Created on 19 Dec 2018  路  2Comments  路  Source: Leaflet/Leaflet

I downloaded the javascript zip and extracted it in a folder on my webserver.

In header.php

============================================= tried to generate a map from the example:

=============================================

receive the error below:
leaflet.js:5 Uncaught TypeError: t.addLayer is not a function

console info seems to point to:
attribution: '© OpenStreetMap contributors'})

Most helpful comment

Randomly spotted this issue and saw the 3 thumbs-down icons on @IvanSanchez's comment without comments. I assume those were 3 unhappy, ungrateful users that had the same issue but sadly did not bother asking for details?

The long explanation and solution should be this (untested, from the top of my head):

<script type="text/javascript">
var map = L.map('mapid').setView([51.505, -0.09], 13);

L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
  attribution: '漏 OpenStreetMap contributors'
}).addTo(mapid);

L.marker([51.5, -0.09]).addTo(mapid).bindPopup(
   'A pretty CSS3 pop. Easily customizable.'
).openPopup();
</script> 

@joefinnerty sadly did not provide a minimal, working example so we have to guess what mapid is. Like Ivan suggested, it probably is a string, probably the CSS id of the map DIV element.

You cannot use that on a Tile Layer's or Marker's addTo() method. You must pass the actual Leaflet Map object to that. Here that would be the map.

So presumably this should work:

<script type="text/javascript">
var map = L.map('mapid').setView([51.505, -0.09], 13);

L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
  attribution: '漏 OpenStreetMap contributors'
}).addTo(map);

L.marker([51.5, -0.09]).addTo(map).bindPopup(
   'A pretty CSS3 pop. Easily customizable.'
).openPopup();
</script> 

All 2 comments

.addTo() needs an instance of L.Map, not a string.

Randomly spotted this issue and saw the 3 thumbs-down icons on @IvanSanchez's comment without comments. I assume those were 3 unhappy, ungrateful users that had the same issue but sadly did not bother asking for details?

The long explanation and solution should be this (untested, from the top of my head):

<script type="text/javascript">
var map = L.map('mapid').setView([51.505, -0.09], 13);

L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
  attribution: '漏 OpenStreetMap contributors'
}).addTo(mapid);

L.marker([51.5, -0.09]).addTo(mapid).bindPopup(
   'A pretty CSS3 pop. Easily customizable.'
).openPopup();
</script> 

@joefinnerty sadly did not provide a minimal, working example so we have to guess what mapid is. Like Ivan suggested, it probably is a string, probably the CSS id of the map DIV element.

You cannot use that on a Tile Layer's or Marker's addTo() method. You must pass the actual Leaflet Map object to that. Here that would be the map.

So presumably this should work:

<script type="text/javascript">
var map = L.map('mapid').setView([51.505, -0.09], 13);

L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
  attribution: '漏 OpenStreetMap contributors'
}).addTo(map);

L.marker([51.5, -0.09]).addTo(map).bindPopup(
   'A pretty CSS3 pop. Easily customizable.'
).openPopup();
</script> 
Was this page helpful?
0 / 5 - 0 ratings

Related issues

walterfn2 picture walterfn2  路  4Comments

timwis picture timwis  路  3Comments

jblarsen picture jblarsen  路  3Comments

onethread picture onethread  路  3Comments

ssured picture ssured  路  3Comments