gpt4 book ai didi

javascript - Yandex map - 获取交通数据

转载 作者:行者123 更新时间:2023-11-28 07:38:36 31 4
gpt4 key购买 nike

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

我在 map 上显示交通数据的简单代码如下

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

最佳答案

没有合法的方法可以做到这一点。论坛上有一些注释,人们使用 Yandex map 的内部 api,但据我所知,这些注释很快就被 Yandex 团队禁止了。 Yandex 不会为您提供与 map 分开的任何流量数据。我建议您查看 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/

关于javascript - Yandex map - 获取交通数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28315678/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com