Load Geojson Source File and create Layer for features with same properties










0















I did this example https://www.mapbox.com/mapbox-gl-js/example/filter-markers/ but with polygons from geojson data. It works but when I try to load the geojson data from a file, I will get an error below




Uncaught TypeError: Cannot read property 'forEach' of undefined




mapboxgl.accessToken = '<my token>';
//var places = "type":"FeatureCollection","features":["type":"Feature","properties":"icon":"Mannheim","geometry":"type":"Polygon","coordinates":[[[8.931884765625,48.73445537176822],[9.876708984375,48.73445537176822],[9.876708984375,49.24629332459796],[8.931884765625,49.24629332459796],[8.931884765625,48.73445537176822]]],"type":"Feature","properties":"icon":"Lindau","geometry":"type":"Polygon","coordinates":[[[11.22802734375,48.36354888898689],[12.359619140624998,48.36354888898689],[12.359619140624998,49.088257784724675],[11.22802734375,49.088257784724675],[11.22802734375,48.36354888898689]]]]
var places = "yes.geojson";


var filterGroup = document.getElementById('filter-group');
var map = new mapboxgl.Map(
container: 'map',
style: 'mapbox://styles/mapbox/light-v9',
center: [10.307, 50.543],
zoom: 5.45
);

map.on('load', function()
// Add a GeoJSON source containing place coordinates and information.
map.addSource("places",
"type": "geojson",
"data": places
);

places.features.forEach(function(feature)
var symbol = feature.properties['icon'];
var layerID = 'poi-' + symbol;

// Add a layer for this symbol type if it hasn't been added already.
if (!map.getLayer(layerID))
map.addLayer(
"id": layerID,
"type": "fill",
"source": "places",
"paint":
"fill-color": "#2E88D6",
"fill-opacity": 0.4
,
"filter": ["==", "icon", symbol]
);

// Add checkbox and label elements for the layer.
var input = document.createElement('input');
input.type = 'checkbox';
input.id = layerID;
input.checked = true;
filterGroup.appendChild(input);

var label = document.createElement('label');
label.setAttribute('for', layerID);
label.textContent = symbol;
filterGroup.appendChild(label);

// When the checkbox changes, update the visibility of the layer.
input.addEventListener('change', function(e)
map.setLayoutProperty(layerID, 'visibility',
e.target.checked ? 'visible' : 'none');
);

);
);


if I set var places = "yes.geojson;" it did not work anymore.










share|improve this question
























  • How are loading the geojson file? I feel that your map is trying to render before the json file is fully loaded

    – leelum1
    Nov 19 '18 at 12:33















0















I did this example https://www.mapbox.com/mapbox-gl-js/example/filter-markers/ but with polygons from geojson data. It works but when I try to load the geojson data from a file, I will get an error below




Uncaught TypeError: Cannot read property 'forEach' of undefined




mapboxgl.accessToken = '<my token>';
//var places = "type":"FeatureCollection","features":["type":"Feature","properties":"icon":"Mannheim","geometry":"type":"Polygon","coordinates":[[[8.931884765625,48.73445537176822],[9.876708984375,48.73445537176822],[9.876708984375,49.24629332459796],[8.931884765625,49.24629332459796],[8.931884765625,48.73445537176822]]],"type":"Feature","properties":"icon":"Lindau","geometry":"type":"Polygon","coordinates":[[[11.22802734375,48.36354888898689],[12.359619140624998,48.36354888898689],[12.359619140624998,49.088257784724675],[11.22802734375,49.088257784724675],[11.22802734375,48.36354888898689]]]]
var places = "yes.geojson";


var filterGroup = document.getElementById('filter-group');
var map = new mapboxgl.Map(
container: 'map',
style: 'mapbox://styles/mapbox/light-v9',
center: [10.307, 50.543],
zoom: 5.45
);

map.on('load', function()
// Add a GeoJSON source containing place coordinates and information.
map.addSource("places",
"type": "geojson",
"data": places
);

places.features.forEach(function(feature)
var symbol = feature.properties['icon'];
var layerID = 'poi-' + symbol;

// Add a layer for this symbol type if it hasn't been added already.
if (!map.getLayer(layerID))
map.addLayer(
"id": layerID,
"type": "fill",
"source": "places",
"paint":
"fill-color": "#2E88D6",
"fill-opacity": 0.4
,
"filter": ["==", "icon", symbol]
);

// Add checkbox and label elements for the layer.
var input = document.createElement('input');
input.type = 'checkbox';
input.id = layerID;
input.checked = true;
filterGroup.appendChild(input);

var label = document.createElement('label');
label.setAttribute('for', layerID);
label.textContent = symbol;
filterGroup.appendChild(label);

// When the checkbox changes, update the visibility of the layer.
input.addEventListener('change', function(e)
map.setLayoutProperty(layerID, 'visibility',
e.target.checked ? 'visible' : 'none');
);

);
);


if I set var places = "yes.geojson;" it did not work anymore.










share|improve this question
























  • How are loading the geojson file? I feel that your map is trying to render before the json file is fully loaded

    – leelum1
    Nov 19 '18 at 12:33













0












0








0








I did this example https://www.mapbox.com/mapbox-gl-js/example/filter-markers/ but with polygons from geojson data. It works but when I try to load the geojson data from a file, I will get an error below




Uncaught TypeError: Cannot read property 'forEach' of undefined




mapboxgl.accessToken = '<my token>';
//var places = "type":"FeatureCollection","features":["type":"Feature","properties":"icon":"Mannheim","geometry":"type":"Polygon","coordinates":[[[8.931884765625,48.73445537176822],[9.876708984375,48.73445537176822],[9.876708984375,49.24629332459796],[8.931884765625,49.24629332459796],[8.931884765625,48.73445537176822]]],"type":"Feature","properties":"icon":"Lindau","geometry":"type":"Polygon","coordinates":[[[11.22802734375,48.36354888898689],[12.359619140624998,48.36354888898689],[12.359619140624998,49.088257784724675],[11.22802734375,49.088257784724675],[11.22802734375,48.36354888898689]]]]
var places = "yes.geojson";


var filterGroup = document.getElementById('filter-group');
var map = new mapboxgl.Map(
container: 'map',
style: 'mapbox://styles/mapbox/light-v9',
center: [10.307, 50.543],
zoom: 5.45
);

map.on('load', function()
// Add a GeoJSON source containing place coordinates and information.
map.addSource("places",
"type": "geojson",
"data": places
);

places.features.forEach(function(feature)
var symbol = feature.properties['icon'];
var layerID = 'poi-' + symbol;

// Add a layer for this symbol type if it hasn't been added already.
if (!map.getLayer(layerID))
map.addLayer(
"id": layerID,
"type": "fill",
"source": "places",
"paint":
"fill-color": "#2E88D6",
"fill-opacity": 0.4
,
"filter": ["==", "icon", symbol]
);

// Add checkbox and label elements for the layer.
var input = document.createElement('input');
input.type = 'checkbox';
input.id = layerID;
input.checked = true;
filterGroup.appendChild(input);

var label = document.createElement('label');
label.setAttribute('for', layerID);
label.textContent = symbol;
filterGroup.appendChild(label);

// When the checkbox changes, update the visibility of the layer.
input.addEventListener('change', function(e)
map.setLayoutProperty(layerID, 'visibility',
e.target.checked ? 'visible' : 'none');
);

);
);


if I set var places = "yes.geojson;" it did not work anymore.










share|improve this question
















I did this example https://www.mapbox.com/mapbox-gl-js/example/filter-markers/ but with polygons from geojson data. It works but when I try to load the geojson data from a file, I will get an error below




Uncaught TypeError: Cannot read property 'forEach' of undefined




mapboxgl.accessToken = '<my token>';
//var places = "type":"FeatureCollection","features":["type":"Feature","properties":"icon":"Mannheim","geometry":"type":"Polygon","coordinates":[[[8.931884765625,48.73445537176822],[9.876708984375,48.73445537176822],[9.876708984375,49.24629332459796],[8.931884765625,49.24629332459796],[8.931884765625,48.73445537176822]]],"type":"Feature","properties":"icon":"Lindau","geometry":"type":"Polygon","coordinates":[[[11.22802734375,48.36354888898689],[12.359619140624998,48.36354888898689],[12.359619140624998,49.088257784724675],[11.22802734375,49.088257784724675],[11.22802734375,48.36354888898689]]]]
var places = "yes.geojson";


var filterGroup = document.getElementById('filter-group');
var map = new mapboxgl.Map(
container: 'map',
style: 'mapbox://styles/mapbox/light-v9',
center: [10.307, 50.543],
zoom: 5.45
);

map.on('load', function()
// Add a GeoJSON source containing place coordinates and information.
map.addSource("places",
"type": "geojson",
"data": places
);

places.features.forEach(function(feature)
var symbol = feature.properties['icon'];
var layerID = 'poi-' + symbol;

// Add a layer for this symbol type if it hasn't been added already.
if (!map.getLayer(layerID))
map.addLayer(
"id": layerID,
"type": "fill",
"source": "places",
"paint":
"fill-color": "#2E88D6",
"fill-opacity": 0.4
,
"filter": ["==", "icon", symbol]
);

// Add checkbox and label elements for the layer.
var input = document.createElement('input');
input.type = 'checkbox';
input.id = layerID;
input.checked = true;
filterGroup.appendChild(input);

var label = document.createElement('label');
label.setAttribute('for', layerID);
label.textContent = symbol;
filterGroup.appendChild(label);

// When the checkbox changes, update the visibility of the layer.
input.addEventListener('change', function(e)
map.setLayoutProperty(layerID, 'visibility',
e.target.checked ? 'visible' : 'none');
);

);
);


if I set var places = "yes.geojson;" it did not work anymore.







mapbox geojson






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 8:44









Zain Farooq

2,04421030




2,04421030










asked Nov 16 '18 at 7:20









Pascal StaubPascal Staub

11




11












  • How are loading the geojson file? I feel that your map is trying to render before the json file is fully loaded

    – leelum1
    Nov 19 '18 at 12:33

















  • How are loading the geojson file? I feel that your map is trying to render before the json file is fully loaded

    – leelum1
    Nov 19 '18 at 12:33
















How are loading the geojson file? I feel that your map is trying to render before the json file is fully loaded

– leelum1
Nov 19 '18 at 12:33





How are loading the geojson file? I feel that your map is trying to render before the json file is fully loaded

– leelum1
Nov 19 '18 at 12:33












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53333163%2fload-geojson-source-file-and-create-layer-for-features-with-same-properties%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53333163%2fload-geojson-source-file-and-create-layer-for-features-with-same-properties%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

政党

天津地下鉄3号線