Skip to content

Instantly share code, notes, and snippets.

@tristen
Last active August 29, 2015 14:24
Show Gist options
  • Save tristen/1914fd00b59523aac1e0 to your computer and use it in GitHub Desktop.
Save tristen/1914fd00b59523aac1e0 to your computer and use it in GitHub Desktop.
layer group from array
var layers = [
{
label: 'foo',
url: 'foo'
},
{
label: 'bar',
url: 'bar'
}
].reduce(function(memo, layer) {
memo[layer.label] = L.tileLayer(layer.url, {id: layer.label}
return memo;
}, {});
var map = L.map('map');
L.control.layers(layers).addTo(map);
@tristen
Copy link
Author

tristen commented Jul 8, 2015

var layers = require('./src/options').layer;

layers = layers.map(function(layer) {
  layer = L.tileLayer(layer.url, {id: layer.label}
  return layer;
});

var map = L.map('map', {
    layers: layers
});

@amyleew
Copy link

amyleew commented Jul 8, 2015

@tristen what am i doing wrong here

var options = require('./src/lrm_options');
var mapLayer = options.layer;

mapLayer = mapLayer.map(function(layer) {
  layer = L.tileLayer(layer.tileLayer, {id: layer.label}
  return layer;
});

var map = L.map('map', {
  layers: mapLayer
});

image

@tristen
Copy link
Author

tristen commented Jul 8, 2015

Looks like you're missing a parens after L.tileLayer

@amyleew
Copy link

amyleew commented Jul 9, 2015

@tristen

Returning TypeError: Cannot read property 'map' of undefined

for index.js#L11

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment