javascripthtmlyandex-apiyandex-maps

Yandex Maps Api example doesn't work


I want to find a route between two points on Istanbul. For this, I try to run a simple code that taken from this guide. But this code doesn't work. I can't view a route path or map. My code is here.

<title>Quick start. Publishing an interactive map on a page</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="http://api-maps.yandex.ru/2.0-stable/?load=package.standard&lang=ru-RU" type="text/javascript"></script>
<script src="http://yandex.st/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">

    var ymap;
    ymaps.ready(function(){ 
        ymap = new ymaps.Map ("map", {
            center: [41.01771, 28.968484],
            zoom: 10,
            controls: ['zoomControl', 'typeSelector',
                       'geolocationControl', 'trafficControl',
                       'fullscreenControl'] 
        });

        ymaps.route([
            'Maltepe',
            'Kartal'
        ]).then(function (route){
            ymap.geoObjects.add(route);
            }, function(error){
               alert("Ошибка. " + error.status +
                     ":" + error.message);
            }
        );

    });

</script>

Solution

  • The code in the example is correct. It is the page that needs to be fixed. You must add a properly positioned/sized DIV element to the HTML of your page. The id of this element ("map") is passed as a first argument to the constructor new ymaps.Map.

    Here is a minimalistic working HTML and CSS code:

    HTML

    <script src="http://api-maps.yandex.ru/2.1/?lang=ru_RU" type="text/javascript"></script>
    <div id="map"></div>
    

    CSS:

    body {
        height: 100%;
    }
    #map {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
    }
    

    I created a live example for you, see this fiddle