---
metadata:
  - name: generator
    content: Diplodoc Platform v5.39.1
alternate:
  - https://yandex.com.tr/dev/jsapi-v2-1/doc/en/v2-1/examples/cases/direct_geocode.md
  - https://yandex.com.tr/dev/jsapi-v2-1/doc/ru/v2-1/examples/cases/direct_geocode.md
---
> **Documentation Index:** Fetch the complete configuration index at https://yandex.com.tr/dev/jsapi-v2-1/doc/en/llms.txt

# Direct geocoding

<iframe id="LIVE-EXAMPLE" style="width:100%;height: 400px;border-radius: 8px;border: 1px solid rgba(92, 94, 102, 0.14);" frameBorder="0" src="https://yastatic.net/s3/front-maps-static/maps-front-jsapi-v2-1/examples/2/out/s3-cases/en/direct_geocode/index.html" allow="fullscreen"></iframe>

<a target="_blank" rel="noopener noreferrer" style="display: inline-block;margin-top: 10px;cursor: pointer;border-radius: 4px;padding: 9px 12px;background: #151515;color: white;text-decoration: none;font-weight: 500" href="https://codesandbox.io/p/sandbox/3d4ryq?file=index.html">Open in CodeSandbox</a>

<div id="references">
    <a href="https://yandex.ru/dev/jsapi-v2-1/doc/en/v2-1/ref/reference/ready">ready</a>, <a href="https://yandex.ru/dev/jsapi-v2-1/doc/en/v2-1/ref/reference/Map">Map</a>,
    <a href="https://yandex.ru/dev/jsapi-v2-1/doc/en/v2-1/ref/reference/geocode">geocode</a>,
    <a href="https://yandex.ru/dev/jsapi-v2-1/doc/en/v2-1/ref/reference/map.GeoObjects">map.GeoObjects</a>
</div>
<p>
    The API can determine the coordinates of an object by its name (<a href="https://yandex.ru/dev/jsapi-v2-1/doc/en/v2-1/dg/concepts/geocoding">forward geocoding</a>)
    and the name of an object by its coordinates (<a href="https://yandex.ru/dev/jsapi-v2-1/doc/en/v2-1/dg/concepts/geocoding">reverse geocoding</a>).
</p>
<p>
    Both types of geocoding are performed using the <a href="https://yandex.ru/dev/jsapi-v2-1/doc/en/v2-1/ref/reference/geocode">geocode</a> function. The result returned by this function can be immediately placed on the map.
</p>
<p>
    This example uses forward geocoding: to mark Nizhny Novgorod on the map,
    we determine the coordinates of its center, and then add a placemark with the found coordinates to the map.
</p>
<p>
    The example shows how to work with data returned by the geocoder: how to add it to the map or, for example,
    how to extract the necessary information about an object.
</p>

{% list tabs %}

-   index.html

    ```html
    <!DOCTYPE html>

    <html>
        <head>
            <title>Examples. Forward geocoding</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/
        -->
            
            
            
        </head>
        <body>
            <div id="map" />
        </body>
    </html>
    ```

-   direct_geocode.js

    ```js
    ymaps.ready(init);

    function init() {
        var myMap = new ymaps.Map("map", {
            center: [55.753994, 37.622093],
            zoom: 9,
        });

        // Finding coordinates of the center of Nizhny Novgorod.
        ymaps
            .geocode("Nizhny Novgorod", {
                /**
                 * Request options
                 * @see https://api.yandex.com/maps/doc/jsapi/2.1/ref/reference/geocode.xml
                 */
                /**
                 * Sorting the results from the center of the map window
                 *  boundedBy: myMap.getBounds(),
                 *  strictBounds: true,
                 *  Together with the boundedBy option, the search will be strictly inside the area specified in boundedBy.
                 *  If you need only one result, this will minimize the user's traffic.
                 */
                results: 1,
            })
            .then(function (res) {
                // Selecting the first result of geocoding.
                var firstGeoObject = res.geoObjects.get(0),
                    // The coordinates of the geo object.
                    coords = firstGeoObject.geometry.getCoordinates(),
                    // The viewport of the geo object.
                    bounds = firstGeoObject.properties.get("boundedBy");

                firstGeoObject.options.set(
                    "preset",
                    "islands#darkBlueDotIconWithCaption"
                );
                // Getting the address string and displaying it in the geo object icon.
                firstGeoObject.properties.set(
                    "iconCaption",
                    firstGeoObject.getAddressLine()
                );

                // Adding first found geo object to the map.
                myMap.geoObjects.add(firstGeoObject);
                // Scaling the map to the geo object viewport.
                myMap.setBounds(bounds, {
                    // Checking the availability of tiles at the given zoom level.
                    checkZoomRange: true,
                });

                /**
                 * All data in the form of a javascript object.
                 */
                console.log(
                    "All the geo object data: ",
                    firstGeoObject.properties.getAll()
                );
                /**
                 * The metadata of the request and geocoder response.
                 * @see https://api.yandex.com/maps/doc/geocoder/desc/reference/GeocoderResponseMetaData.xml
                 */
                console.log(
                    "The metadata of the geocoder response: ",
                    res.metaData
                );
                /**
                 * Metadata of the geocoder returned for the found object.
                 * @see https://api.yandex.com/maps/doc/geocoder/desc/reference/GeocoderMetaData.xml
                 */
                console.log(
                    "Geocoder metadata: ",
                    firstGeoObject.properties.get(
                        "metaDataProperty.GeocoderMetaData"
                    )
                );
                /**
                 * The accuracy of the response (precision) is only returned for houses.
                 * @see https://api.yandex.com/maps/doc/geocoder/desc/reference/precision.xml
                 */
                console.log(
                    "precision",
                    firstGeoObject.properties.get(
                        "metaDataProperty.GeocoderMetaData.precision"
                    )
                );
                /**
                 * The type of found object (kind).
                 * @see https://api.yandex.com/maps/doc/geocoder/desc/reference/kind.xml
                 */
                console.log(
                    "Type of geo object: %s",
                    firstGeoObject.properties.get(
                        "metaDataProperty.GeocoderMetaData.kind"
                    )
                );
                console.log(
                    "Object name: %s",
                    firstGeoObject.properties.get("name")
                );
                console.log(
                    "Object description: %s",
                    firstGeoObject.properties.get("description")
                );
                console.log(
                    "Full object description: %s",
                    firstGeoObject.properties.get("text")
                );
                /**
                 * Direct methods for working with the geocoding results.
                 * @see https://tech.yandex.com/maps/doc/jsapi/2.1/ref/reference/GeocodeResult-docpage/#getAddressLine
                 */
                console.log("\nState: %s", firstGeoObject.getCountry());
                console.log(
                    "Municipality: %s",
                    firstGeoObject.getLocalities().join(", ")
                );
                console.log(
                    "Address of the object: %s",
                    firstGeoObject.getAddressLine()
                );
                console.log(
                    "Name of the building: %s",
                    firstGeoObject.getPremise() || "-"
                );
                console.log(
                    "Building number: %s",
                    firstGeoObject.getPremiseNumber() || "-"
                );

                /**
                 * To add a placemark with its own styles and balloon content at the coordinates found by the geocoder, create a new placemark at the coordinates of the found placemark and add it to the map in place of the found one.
                 */
                /**
                 var myPlacemark = new ymaps.Placemark(coords, {
                 iconContent: 'my placemark',
                 balloonContent: 'Content of the <strong>my placemark</strong> balloon'
                 }, {
                 preset: 'islands#violetStretchyIcon'
                 });
    
                 myMap.geoObjects.add(myPlacemark);
                 */
            });
    }
    ```

{% endlist %}
