Yandex地图 - 获取交通数据

时间:2015-02-04 07:21:51

标签: javascript traffic yandex-api yandex-maps

我尝试使用yandex地图在特定时间和地点获取交通数据。我看this page(yandex地图的api)。我可以在自己的地图上显示交通数据。使用geocoding,我从yandex贴图中提取一些数据(地名,坐标等)。但我不知道如何仅提取流量数据。我该怎么做? yandex map api上有任何功能吗?

我在地图上显示交通数据的简单代码在

之下
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);
    }

1 个答案:

答案 0 :(得分:0)

没有合法的方法可以做到这一点。在论坛上有一些注意事项,人们使用Yandex地图的内部api,但据我所知,它们被Yandex团队迅速禁止。 Yandex不会为您提供与地图分开的任何交通数据。我建议您查看Google Maps Api,在那里您可以获得您指定的路线上的交通信息。例如:

    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

有关详细信息,请查看https://developers.google.com/maps/