javascript - Mapbox directions: how to extract directions data from result -


i using mapbox directions in javascript app return geometry , routing data (between 2 lat/long pairs) browser. trying display -- -- route polyline connecting them on map, can not access directions data result object though data there.

here request code:

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

note console.dir line. see in console route:

    _inithookscalled:true     _query:null     _requests:array[0]     _waypoints:array[0]     destination:object     directions:object     options:object     origin:object 

this see in console route.destination, route.options , route.origin objects:

    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"} 

but when try see route.directions, undefined.

    console.log(route.directions);       => undefined 

when "opening" whole route object in console (via console.dir) see directions data looking for:

    directions:object         destination:object         origin:object         routes:array[2]             0:object                 distance:10460                 duration:862                 geometry:object                 steps:array[26]                 ... 

why "undefined" when accessing route.directions, when can see contents of other "sub-objects" (e.g. route.options) using same dot syntax?

finally, when "stringify" route object var routejson = json.stringify(route); see this:

    {"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":{}} 

again, no directions content! thoughts appreciated!

where finding api query function? according directions api documentation on github, should calling this:

var mapboxclient = new mapboxclient('accesstoken'); mapboxclient.getdirections(   [     { latitude: 33.6, longitude: -95.4431 },     { latitude: 33.2, longitude: -95.4431 } ],   function(err, res) {   // res document directions });  // 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); }); 

i suspect passing geometry: 'polyline need make sure geometry information included in response.


Comments