javascript - Map Route Direction Zoom to Segment -


basically trying zoom route segment when getting direction on onemap. here javascript codes trying plot route , zoom route segment:

function getdirections() { var routedata = new route; var = document.getelementbyid('txtfrom').value var = document.getelementbyid('txtto').value //draw out line cordinate cordinate routedata.routestops = + ";" + to;  //what type of mode routedata.routemode = "drive"; //can draw out untill following coordiante routedata.barriers = '36908.388637,35897.420831'; {     if (document.getelementbyid('cbavoid').checked) {         routedata.avoiderp = "1";     }     else         routedata.avoiderp = "0"; } routedata.getroute(showroutedata) }  function showroutedata(routeresults) {     if (routeresults.results == "no results") {         alert("no route found, please try other location.")         return     }     $('#divcomputeddirection').show();     directions = routeresults.results.directions[0];     directionfeatures = directions.features;     var routesymbol = new esri.symbol.simplelinesymbol().setcolor(new dojo.color([0, 0, 255, 0.5])).setwidth(4);     var mergedgeometry = new esri.geometry.polyline()      mergedgeometry.addpath(routeresults.results.routes.features[0].geometry.paths[0])     onemap.map.graphics.add(new esri.graphic(mergedgeometry, routesymbol));     //display total time , distance of route                        var htmlstr = "";     htmlstr += "<img class='close-image' onclick='closedirectionresultdiv();' alt='close' src='img/closedirectionresult.png' />";     htmlstr += "<span style='font-weight:bold;'><br /> &nbsp; total distance: " + math.round(directions.summary.totallength) + "km" + "<br /> &nbsp; total time: " + math.round(directions.summary.totaltime) + "mins <br/></span>";      document.getelementbyid("divcomputeddirection").innerhtml = htmlstr;      //list directions , create hyperlinks each route segment     (var = 0; < directions.features.length; i++) {         var feature = directions.features[i]         document.getelementbyid("divcomputeddirection").innerhtml += '<a href="javascript:zoomtosegment(' + + ')" style="font-size: 11px"><br>' + parseint(parseint(i) + 1) + ". " + feature.attributes.text + " (" + formatdistance(feature.attributes.length, "miles") + ", " + formattime(feature.attributes.time) + ") " + '</a>';     } }  //zoom appropriate segment when user clicks hyperlink in directions list function zoomtosegment(index) { var segment = directionfeatures[index]; map.setextent(segment.geometry.getextent(), true); if (!segmentgraphic) {     segmentgraphic = map.graphics.add(new esri.graphic(segment.geometry, segmentsymbol)); } else {     segmentgraphic.setgeometry(segment.geometry); } 

}

it did plot route , show directions. when click on direction , zoom segement, throws me error message uncaught typeerror: cannot call method 'getextent' of undefined.

i wonder why so. in advance.

the root cause of error you're trying extent of .geometry property doesn't exist - part relatively easy. problem, think, you're looking geometry of each segment of journey, , return onemap's routetask doesn't give directly.

the geometry entire route in

routeresults.results.routes.features[0].geometry.paths[0] 

and individual segments in 1 of esri's fun compressed formats in value:

routeresults.results.directions[x].features[y].compressedgeometry 

there's documentation , c# code compressed format here:

http://resources.esri.com/help/9.3/arcgisengine/arcobjects/esrinetworkanalyst/inacompactstreetdirection_compressedgeometry.htm

it should relatively easy port c# code js if need geometry of individual segments.

onemap have full working example here shows how process results routetask, unfortunately don't attempt extract compressedgeometry field.


edit: more sample code esri here, examples in c#/java/python.


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -