使用 Mapzen 将转弯数据作为字符串或 JSON 返回

Returning turn-by-turn data as a string or JSON with Mapzen

我一直在使用这个库从使用 Mapzen's turn-by-turn data API 的设定路线获取机动数据。虽然我在设置地图和路线方面没有问题,但我很难访问 object/string 来保存车辆在每个点(左、右等)需要转弯的位置,因此我可以将其与 GPS 一起使用.将其作为字符串或 JSON 获取将是完美的,我只是现在不知道该怎么做。

我发现有一种方法可以从文档中做到这一点,但我要么不理解它,要么试图以错误的方式去做,所以我想知道是否有更有经验的人可以帮助我或给予我有一些关于如何实现这一目标的想法。

到目前为止,这是我的脚本代码(所有文件都附在下面):

// Create a map. Targets div with id 'map'.
var map = L.map('map');

var layer = Tangram.leafletLayer({
  scene: 'https://raw.githubusercontent.com/tangrams/refill-style/gh-pages/refill-style.yaml',
  attribution: '<a href="https://mapzen.com/tangram" target="_blank">Tangram</a> | <a href="http://www.openstreetmap.org/about" target="_blank">&copy; OSM contributors | <a href="https://mapzen.com/" target="_blank">Mapzen</a>',
}); // End of layer.

// Adds the styles and attributions to the map.
layer.addTo(map);

// Set the default view of the map (Bristol).
// No need to use this when a route is being displayed.
//map.setView([51.4545,-2.5879], 16);

// Sets a route and adds it to the map, it requires two points and their respective latitude and longitude.
L.Routing.control({
      waypoints: [
        L.latLng(51.4998,-2.5468),
        L.latLng(51.5055,-2.5603)
      ]
    }).addTo(map);

// I used a log to try and find out which object has the turn-by-turn data. No luck so far.
console.log(L.Routing.mapzen('mapzen-xxxxxxx', `{costing:'auto'}));

我目前拥有的项目文件(在 GitHub 上 post 的末尾): https://github.com/mapzen/lrm-mapzen/issues/72

感谢您的阅读,如果有人帮助我,我将不胜感激。

交叉发布来自上述 github issue 的答案:

var sampleLatLngs = [L.Routing.waypoint(L.latLng(51.4998,-2.5468)), L.Routing.waypoint(L.latLng(51.5055,-2.5603))];

// You can grab free api key at https://mapzen.com/developers
var router = L.Routing.mapzen('your-api-key', {costing: 'pedestrian'});

// We are just going to print out route turned from the server
var sampleFunction = function(err, routes) {
    // Check your browser console
    console.log(routes);
}

router.route(sampleLatLngs, sampleFunction);

If you check your browser, you will see the response from the server. instructions property probably has the info you want.