Only return selected GeoJSON elements in Leaflet -
i have code osm alley elements in map , has button print elements retrieved overpass api.
instead retrieving elements, me able :
- select multiple elements on map clicking on want (the selected elements marked different color blue).
- return selected elements.
here javascript code:
// initializing map var map = new l.map('map', { center: l.latlng(46.827, -71.227), zoom: 15, zoomcontrol: false, layers: l.tilelayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png') }); map.addcontrol(l.control.zoom({position:'topleft'})); var geolayer = l.geojson().addto(map); //getting elements on map $.ajax({ data: "data=[out:json];way[highway=service][service=alley](46.822161505913414,-71.23554468154907,46.83211547933473,-71.21927976608276);(._;>;);out;", type: 'post', datatype: 'json', url: 'http://overpass-api.de/api/interpreter', success: function(json) { //loading warning... $('#preloader').hide(); $('#preloader') .ajaxstart(function(){ $(this).show(); }).ajaxstop(function(){ $(this).hide(); }); //putting elements on map var geojson = osmtogeojson(json); geolayer = l.geojson(geojson, { oneachfeature: function (feature, layer) { //bind click layer.on({ click: null //add property feature "selected: true" if selected false , vice versa?????? }); //change style // ??? if selected false, keep default brue alleys, selected true go light green? } }).addto(map); } }); // printing elements function getallelements() { var allmarkersobjarray = [];//new array(); var allmarkersgeojsonarray = [];//new array(); $.each(map._layers, function (ml) { //console.log(map._layers) if (map._layers[ml].feature) { allmarkersobjarray.push(this) allmarkersgeojsonarray.push(json.stringify(this.togeojson())) } }) console.log(allmarkersobjarray); alert("total markers : " + allmarkersgeojsonarray.length + "\n\n" + allmarkersgeojsonarray + "\n\n see console object view of array" ); } $(".get-elements").on("click", getallelements);
and here html:
<!doctype html public "-//w3c//dtd xhtml 1.0 strict//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <link rel="stylesheet" href="//cdn.jsdelivr.net/leaflet/0.7.3/leaflet.css"/> <style> #map { float:left; width:900px; height:600px; } </style> </head> <body> <h4>here alleys. please select alleys use , click send.</h4> <div id="preloader">chargement...</div> <div id="map"></div> <input class="get-elements" type="button" value="send" /> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <script src="//cdn.jsdelivr.net/leaflet/0.7.3/leaflet.js"></script> <script src="/assets/javascripts/leaflet/osmtogeojson.js"></script> <script>code here</script> </body> </html>
thanks help!
on each feature can set new property selected true , color follows:
layer.on('click', function (e) { //set feature selected, can use layer.feature.properties e.target.feature.properties.selected = true; //set style, ever style option want layer.setstyle({opacity:1,width:3, fillcolor:"#0080ff"}); }
to select features having property selected:true, (didn't try or test , may buggy)
$.each(map._layers, function (ml) { //console.log(map._layers) if (map._layers[ml].feature && map._layers[ml].feature.properties.selected === true) { allmarkersobjarray.push(this) allmarkersgeojsonarray.push(json.stringify(this.togeojson())) } })
ps> no need use $.each here, use plain loop
Comments
Post a Comment