Leaflet Ajax JSON file Add to Map as Overlay -
i have json file not formatted in way meet geojson standards. trying load data parse on map. able create markers data , add them:
$.getjson( tsdata, function ( data ) { //loop through data $.each( data.response.docs, function ( key, val ) { //console.log(val); if ( typeof val.mc_geo !== 'undefined' ) { //lat lng isn't consitently 1, getting first only. var geo = val.mc_geo; var glat = geo.tostring().split( ',' )[ 0 ]; var glng = geo.tostring().split( ',' )[ 1 ]; //add markers map var marker = l.marker( [ glat, glng ],{icon: tsmarker,} ).bindpopup( "<p><strong><a href='" + val.filename + "'>" + val.title + "</a></strong><hr/><br/><strong>abstract:</strong> " + truncatestring( val.abstract, 350 ) + "</p><p><strong>author(s): </strong>" + val.author + "<p><strong>station:</strong> " + val.station_primary_full + "</p>" ); markerarray.push( marker ); } } ); layer_group = l.layergroup(markerarray); map.addlayer(layer_group); });
this adds marker layers (layer_group) map no problem. however, if try , add them the:
l.control.layers(basemaps, overlaymaps).addto(map);
i error. basically, l.control.layers not seeing layer_group. can define before getjson call, l.control.layers not show layer full of data though "checked" in layers panel... there no markers displayed.
how dynamically create marker layer group , have control on showing (on / off) in l.control.layers?
it sounds add layer_group
layers control while empty ($.getjson
works asynchronously), re-assign it, there no longer reference between layers control , markers have instantiated.
2 easy workarounds:
add
layer_group
layers control after built:mylayerscontrol.addoverlay(layer_group, "markers")
use placeholder layer group in layers control, add markers (
mylayergroup.addlayer(marker)
) or addlayer_group
@ end (mylayergroup.addlayer(layer_group)
)
Comments
Post a Comment