Displaying geocoding results

Open in CodeSandbox

The geoXml.load function allows loading geocoding results as a YMapsML document.

After data is loaded, the function converts it to a GeoObjectCollection that can be displayed on the map.

<!DOCTYPE html>
<html>
    <head>
        <title>YMapsML example. Displaying geocoding results.</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://api-maps.yandex.ru/2.1/?lang=en_RU&amp;apikey=<your API-key>"
            type="text/javascript"
        ></script>
        <script
            src="ymapsml_showhttpgeocoderresult.js"
            type="text/javascript"
        ></script>
        <style>
            html,
            body,
            #map {
                width: 100%;
                height: 100%;
                padding: 0;
                margin: 0;
            }
        </style>
    </head>
    <body>
        <div id="map"></div>
    </body>
</html>
ymaps.ready(init);

function init() {
    // Creating an instance of the map.
    var myMap = new ymaps.Map(
        "map",
        {
            center: [55.76, 37.64],
            zoom: 10,
        },
        {
            searchControlProvider: "yandex#search",
        }
    );

    // Loading geocoding results.
    ymaps.geoXml
        .load("http://geocode-maps.yandex.ru/1.x/?geocode=poselok Ray")
        .then(
            function (res) {
                res.geoObjects.each(function (item) {
                    // Boundaries of the map area that contains the found object.
                    var bounds = item.properties.get("boundedBy");
                    // Adding a geo object to the map.
                    myMap.geoObjects.add(item);
                    // Changing the map viewport.
                    myMap.setBounds(bounds);
                });
            },
            // Called if data loading was unsuccessful.
            function (error) {
                alert(
                    "When loading the YMapsML file, the following error occurred: " +
                        error
                );
            }
        );
}