Made on samples from the sandbox to add an array of points and create your own drop-down menu. At the moment, the points on the map are placed, but how to handle the JSON array and pull the necessary information out of it has not yet understood. At the moment, I got to this, but it does not work

$.getJSON('geojson.json').done(function (json){ for (var y = 0; y < json.features.length; y++) { var b = json.features[y]["properties"].address, c = json.features[y]["geometry"].coordinates; var listBoxItem = new ymaps.control.ListBoxItem({ data: { content: b, center: c } }) } }); 

I would be grateful for help, or at least for a hint where to spy and what can be adapted to my goal. If necessary - a demo , but without JSON it can be useless. Cross-domain requests for the demo do not work

    1 answer 1

    The problem is that the function passed as $.getJSON('geojson.json').done() argument is executed asynchronously. At this point, your listBox will be created with an empty list and will need to be filled ( listBox.add() ). In addition, your listBox is created dynamically and the materialize drop-down list has already passed the initialization by the list, it will be necessary to initialize it separately after filling.

    Given the above, the list filling function should look like this:

     a = $.getJSON('geojson.json').done(function (json) { for (var y = 0; y < json.features.length; y++) { var b = json.features[y]["properties"].address, c = json.features[y]["geometry"].coordinates; var listBoxItem = new ymaps.control.ListBoxItem({ data: { content: b, center: c } }); // ДобавляСм ΠΏΡƒΠ½ΠΊΡ‚ Π² список listBox.add(listBoxItem); } // Π˜Π½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·ΠΈΡ€ΡƒΠ΅ΠΌ список $('.dropdown-button').dropdown({}); }); 

    In addition, the definition of the listBox variable listBox be moved above this section of the code and make your geojson.json valid (remove the first line from there)

    • unexpectedly detailed explanation. Can I have a couple of clarifications? In view of the fact that I know little about the work of JSON, I would like to clarify whether it is possible to avoid removing the first line from the JSON file, since it is later used in the initialization of points on the map, or will it not affect anything? - spirit-infernal
    • @ spirit-infernal I meant a string of the form header(... If JSON is invalid, it will not load (JSON is a slightly more formalized form of JavaScript records of arrays and objects β€” if in a simple way. In fact, you should know about its work is nothing) - tutankhamun
    • geojson.json does not have such a string in the composition, I did it through the service for creating labels from the xls file with the names of points. here is its title: {"type": "FeatureCollection", "features": [{"type": "Feature", "geometry": {"type": "Point", "coordinates": ["82.763045", " 52.49635 "]}," properties ": {" id ":" 0 "," address ":" \ u0410 \ u043b \ u0435 \ u0439 \ u0441 \ u043a, \ u0443 \ u043b. \ U041e \ u043b \ u0435 \ u0448 \ u043a \ u043e, 40 \ u0415 "," index ":" 0 "}," options ": {" preset ":" islands # violetGovernmentIcon "}}, is it worth it to clean? - spirit-infernal
    • I myself also tried to investigate the moment about why at the moment something like that is displayed as content: l {state: h, data: h, options: Object, events: s, _itemImplementation: n ...} _enabled: true _itemImplementation: n _layout: null _layoutDeferred: null _selected: undefined data: h eventProxy: l events: s options: Object state: h proto : i as far as I understand, this is the object of the map constructor. But what to do with him has not yet been able to understand. I try your method - spirit-infernal
    • @ spirit-infernal There is no need to remove anything from the above piece. The provided jsfiddle was geojson.json with the first line starting with header( - tutankhamun