【发布时间】:2014-07-29 13:02:18
【问题描述】:
基本上,我在 OneMap 上获取方向时尝试缩放到某些路线段。这是我尝试绘制路线并缩放到特定路线段的 JavaScript 代码:
function getDirections() {
var routeData = new Route;
var from = document.getElementById('txtFrom').value
var to = document.getElementById('txtTo').value
//Draw out the line from the cordinate to another cordinate
routeData.routeStops = from + ";" + to;
//What type of mode will it do
routeData.routeMode = "DRIVE";
//can draw out untill the 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 the total time and distance of the route
var htmlStr = "";
htmlStr += "<img class='close-image' onclick='closeDirectionResultDIV();' alt='close' src='img/closeDirectionResult.png' />";
htmlStr += "<span style='font-weight:bold;'><br /> Total distance: " + Math.round(directions.summary.totalLength) + "km" + "<br /> Total time: " + Math.round(directions.summary.totalTime) + "mins <br/></span>";
document.getElementById("divComputedDirection").innerHTML = htmlStr;
//List the directions and create hyperlinks for each route segment
for (var i = 0; i < directions.features.length; i++) {
var feature = directions.features[i]
document.getElementById("divComputedDirection").innerHTML += '<a href="JavaScript:zoomToSegment(' + i + ')" style="font-size: 11px"><br>' + parseInt(parseInt(i) + 1) + ". " + feature.attributes.text + " (" + formatDistance(feature.attributes.length, "miles") + ", " + formatTime(feature.attributes.time) + ") " + '</a>';
}
}
//Zoom to the appropriate segment when the user clicks a hyperlink in the 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);
}
}
它确实绘制了路线并显示了所有方向。但是当我单击某个方向并缩放到分段时,它会向我抛出一条错误消息,即Uncaught TypeError: Cannot call method 'getExtent' of undefined。
我想知道为什么会这样。提前致谢。
【问题讨论】:
-
有什么线索可以解决这个问题吗?
-
你在哪里定义
directionFeatures?如果您没有在showRouteData()函数之外定义它,那么它是该函数的局部变量 - 当事件处理程序触发zoomToSegment时,directionFeatures不再存在。 -
@Juffy 我将其定义为全局变量。你有什么办法解决这个问题吗?
-
如果没有看到一个工作示例,不 - 我会启动一个调试器(例如 Chrome 的 F12 开发工具)并查看
directionFeatures数组中的内容zoomToSegment函数执行时. -
@Juffy 它返回一个具有 ETA 属性的对象:-2209161600000 长度:0.1689308050723526 机动类型:“esriDMTTurnRight”文本:“在 TIONG POH ROAD 右转以留在 TIONG BAHRU ROAD”时间:0.253399999999996 . getExtent() 实际上做了什么?和 segment.geometry 返回我一个未定义的。
标签: javascript dictionary esri segment arcgis-js-api