我在javascript应用程序中使用Mapbox Directions来成功地将几何体和路由数据(在两个纬度/长度对之间)返回到浏览器。我试图显示 - 最终 - 在地图上连接它们的路线折线,但即使数据存在,我也无法从结果对象访问路线数据。
以下是请求代码:
L.mapbox.accessToken ='pk.eyJ1Ij...9Uk_Z8JqMOQ';
L.mapbox.id = 'tom...ibb';
var startLatLng = L.latLng(38.935899, -77.022353);
var endLatLng = L.latLng(38.90, -77.10);
var directions = L.mapbox.directions();
directions.setOrigin(startLatLng);
directions.setDestination(endLatLng);
var route = directions.query();
console.dir(route);
请注意console.dir行。这就是我在控制台中看到的路由:
_initHooksCalled:true
_query:null
_requests:Array[0]
_waypoints:Array[0]
destination:Object
directions:Object
options:Object
origin:Object
这是我在控制台中看到的route.destination,route.options和route.origin对象:
console.log(route.destination);
=> Object {type: "Feature", geometry: Object, properties:...
console.log(route.origin);
=> Object {type: "Feature", geometry: Object, properties:...
console.log(route.options);
=> Object {units: "imperial"}
但是当我尝试查看route.directions时,它是未定义的。
console.log(route.directions);
=> undefined
当"开放"控制台中的整个路由对象(通过console.dir)我看到了我要查找的路线数据:
directions:Object
destination:Object
origin:Object
routes:Array[2]
0:Object
distance:10460
duration:862
geometry:Object
steps:Array[26]
...
为什么这会是"未定义"当使用route.directions访问它时,特别是当我可以看到其他"子对象"的内容时(例如route.options)使用相同的点语法?
最后,当我" stringify"路线对象var routeJson = JSON.stringify(route);
我看到了:
{"options":{
"units":"imperial"
},
"_waypoints":[],
"_initHooksCalled":true,
"origin":{
"type":"Feature",
"geometry":{
"type":"Point",
"coordinates":[-77.022353,38.935899]
},
"properties":{"query":[-77.022353,38.935899]}
},
"destination":{
"type":"Feature",
"geometry":{
"type":"Point",
"coordinates":[-77.1,38.9]},
"properties":{"query":[-77.1,38.9]}
},
"_requests":[],
"_query":{}}
再次,没有指示内容!任何想法都将不胜感激!
答案 0 :(得分:0)
您在哪里找到具有query
功能的API?根据{{3}},您应该这样称呼它:
var mapboxClient = new MapboxClient('ACCESSTOKEN');
mapboxClient.getDirections(
[
{ latitude: 33.6, longitude: -95.4431 },
{ latitude: 33.2, longitude: -95.4431 } ],
function(err, res) {
// res is a document with directions
});
// With options
mapboxClient.getDirections([
{ latitude: 33.6875431, longitude: -95.4431142 },
{ latitude: 33.6875431, longitude: -95.4831142 }
], {
profile: 'mapbox.walking',
instructions: 'html',
alternatives: false,
geometry: 'polyline'
}, function(err, results) {
console.log(results.origin);
});
我怀疑传递geometry: 'polyline
是您需要确保在响应中包含几何信息所需的。