V
V
vector20142020-06-08 23:55:34
JavaScript
vector2014, 2020-06-08 23:55:34

How to change json file path in jQuery.getJSON and load new json file?

There are several json files, of which info on Yandex tags is loaded. When you select a city, the map should change.
I thought to do it this way: each city has an attribute that corresponds to the name of the json file. When you click on this city, the path to the json file changes in the script and the map is reloaded from the new json array.

Here is a code snippet:

jQuery.getJSON('/wp-content/uploads/Открытие/1.json', function (json) {
                                    var geoObjects = ym.geoQuery(json)
                                        .addToMap(myMap)
                                    myMap.setCenter(geoObjects.get(0).geometry.getCoordinates());
                                    myMap.setBounds(myMap.geoObjects.getBounds(),{checkZoomRange:true, zoomMargin:9});
                                });

"1.json" is the name of the file. By clicking, you just need to change the number "1" to "2" and reload the map

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Dagaev, 2020-06-09
@sodope_69

Are the files in order?

i=1;
var path = '/wp-content/uploads/Открытие/'+i+'.json';

function test() {
  //code...
  i++;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question