javascripttrafficyandex-apiyandex-maps

Yandex Map - Get traffic data


I try to get traffic data at a spesific time and place using yandex maps. I look this page( api of yandex maps ). And I can show traffic data on my own map. Using geocoding, I extract some data(place names, coordinates etc) from yandex maps. But I don't know how I extract only traffic data. How can I do it? Is there any function on yandex map api?

My simple code that shows traffic data on map is below

ymaps.ready(init);
    var myMap, 
        myPlacemark;

    function init(){ 
        myMap = new ymaps.Map ("mapId", {
            center: [28.968484, 41.01771],
            zoom: 7
        }); 

        myPlacemark = new ymaps.Placemark([28.968484, 41.01771], {
            content: 'Moscow!',
            balloonContent: 'Capital of Russia'
        });                     


        // This page should show traffic and the "search on map" tool
        ymaps.load(['package.traffic', 'package.search'], addControls);

        myMap.geoObjects.add(myPlacemark);
    }       

    function addControls(map) {
        myMap.controls.add('trafficControl').add('searchControl');
        var trafficControl = new ymaps.control.TrafficControl({shown: true});
        map.controls.add(trafficControl);
        function updateProvider () {
             trafficControl.getProvider('traffic#actual').update();  
        }
        // We will send a request to update data every 4 minutes.
        window.setInterval(updateProvider, 1 * 60 * 1000);
    }

Solution

  • There is no legal way to do it. There are some notes at forums, where people use internal api of Yandex maps, but they as far as I know rapidly banned by Yandex team. Yandex does not provide you any traffic data separatly from maps. I recommened you to take a look at Google Maps Api, there you could get traffic info on route your have specified. Example:

        final String baseUrl = "http://maps.googleapis.com/maps/api/directions/json";// path to Geocoding API via http
    
        final Map<String, String> params = new HashMap<String, String>();
        params.put("sensor", "false");// if data for geocoding comes from device with sensor
        params.put("language", "en");
        params.put("mode", "driving");// move mode, could be driving, walking, bicycling
        params.put("origin", "Russia, Moscow, Poklonnaya str., 12");// start point of route as address or text value of lon and lat
        params.put("destination", "Russia, Moscow, metro station Park Pobedy");// the same for end point
        final String url = baseUrl + '?' + encodeParams(params);// generate final url
    
        final JSONObject response = JsonReader.read(url);// perform request and read answer where we could also get coordinates
        //results[0]/geometry/location/lng and results[0]/geometry/location/lat
    
        JSONObject location = response.getJSONArray("routes").getJSONObject(0);
        location = location.getJSONArray("legs").getJSONObject(0);
        final String distance = location.getJSONObject("distance").getString("text"); // get distance for route
        final String duration = location.getJSONObject("duration").getString("text"); // get duration for route
    

    For more info just take a look at https://developers.google.com/maps/