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

# Creating a map with the user's location

<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/geolocated_map/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/5nr3xv?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/geolocation">geolocation</a>,
    <a href="https://yandex.ru/dev/jsapi-v2-1/doc/en/v2-1/ref/reference/util.bounds#getCenterAndZoom">util.bounds.getCenterAndZoom</a>
</div>
<p>
    This example demonstrates how to create a map that is initially centered on the user's location.
</p>
<p>
    The "get" method of the <a href="https://yandex.ru/dev/jsapi-v2-1/doc/en/v2-1/ref/reference/geolocation">geolocation</a> function is used for detecting the location. In the parameters, you can set the preferred method of
	location detection (by IP or using the <a href="https://www.w3.org/TR/geolocation-API/">Geolocation API</a>), the time to wait for a response, and so on.
	For more information, see the <a href="https://yandex.ru/dev/jsapi-v2-1/doc/en/v2-1/ref/reference/geolocation#get">description of the method</a>.
</p>
<p>
    When calling this method, the browser tries to determine the geographical position of the user. If successful,
	the result is written in the <a href="https://yandex.ru/dev/jsapi-v2-1/doc/en/v2-1/ref/reference/map.GeoObjects">geoObjects</a> map field; otherwise, there is an error message.
 </p>
 <p>
     In this example, if the location cannot be detected, a map is created with the center in Moscow.
</p>

{% list tabs %}

-   index.html

    ```html
    <!DOCTYPE html>
    <html>
        <head>
            <title>Examples. Creating a map with the user's location</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"></div>
        </body>
    </html>
    ```

-   geolocated_map.js

    ```js
    ymaps.ready(function () {
        var map;
        ymaps.geolocation.get().then(
            function (res) {
                var mapContainer = $("#map"),
                    bounds = res.geoObjects.get(0).properties.get("boundedBy"),
                    // Calculating the viewport for the user's current location.
                    mapState = ymaps.util.bounds.getCenterAndZoom(bounds, [
                        mapContainer.width(),
                        mapContainer.height(),
                    ]);
                createMap(mapState);
            },
            function (e) {
                // If the location cannot be obtained, we just create a map.
                createMap({
                    center: [55.751574, 37.573856],
                    zoom: 2,
                });
            }
        );

        function createMap(state) {
            map = new ymaps.Map("map", state);
        }
    });
    ```

{% endlist %}
