Loading geo data using jQuery and adding it to the map

Open in CodeSandbox

The example shows how to load data using JQuery.getJSON() and display it on the map.

Loaded data is provided in GeoJSON format. To add it to the map, use geoQuery.

Note that the object coordinates are set in the order "longitude, latitude". Since the API uses "latitude, longitude" by default, the "coordorder" parameter with the "lotlang" value must be passed when enabling the API. For more information, see the section Enabling the API.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title>Examples. Loading geo data using jQuery.</title>
        <meta
            http-equiv="Content-Type"
            content="text/html; charset=UTF-8"
        />
        <!--
        Set your own API-key. Testing key is not valid for other web-sites and services.
        Get your API-key on the Developer Dashboard: https://developer.tech.yandex.ru/keys/
    -->
        <script
            src="https://yandex.st/jquery/2.2.3/jquery.min.js"
            type="text/javascript"
        ></script>
        <script src="https://api-maps.yandex.ru/2.1/?lang=en_RU&amp;apikey=<your API-key>&amp;coordorder=longlat"></script>
        <script src="data_load_jquery.js"></script>
        <style type="text/css">
            html,
            body,
            #YMapsID {
                width: 100%;
                height: 100%;
            }
        </style>
    </head>
    <body>
        <div id="YMapsID"></div>
    </body>
</html>
ymaps.ready().done(function (ym) {
    var myMap = new ym.Map(
        "YMapsID",
        {
            center: [55.751574, 37.573856],
            zoom: 10,
        },
        {
            searchControlProvider: "yandex#search",
        }
    );

    jQuery.getJSON("data.json", function (json) {
        /** Saving a reference to the geo objects in case any postprocessing is necessary.
         * @see https://api.yandex.com/maps/doc/jsapi/2.1/ref/reference/GeoQueryResult.xml
         */
        var geoObjects = ym
            .geoQuery(json)
            .addToMap(myMap)
            .applyBoundsToMap(myMap, {
                checkZoomRange: true,
            });
    });
});