2014-06-09 4 views
0

기본적으로 OneMap에 방향을 지정할 때 특정 경로 세그먼트를 확대하려고합니다. 여기에 내가 경로를 플롯 및 특정 경로 세그먼트에 확대하려고 자바 스크립트 코드입니다 :지도 경로 방향 세그먼트로 확대

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 /> &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 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이라는 오류 메시지가 나옵니다.

이유가 궁금합니다. 미리 감사드립니다.

+0

해결할 수있는 단서가 있습니까? –

+0

'directionFeatures'는 어디에 정의합니까? 'showRouteData()'함수 밖에서 정의하지 않으면, 그 함수에 대한 지역 변수입니다. 이벤트 핸들러가'zoomToSegment'를 트리거하면'directionFeatures'는 더 이상 존재하지 않습니다. – Juffy

+0

@Juffy 전역 변수로 정의했습니다. 이 문제를 해결할 생각이 있습니까? –

답변

1

오류의 근본 원인은 존재하지 않는 .geometry 속성의 범위를 얻으려는 것입니다. 그 부분은 비교적 쉽습니다. 제 생각에이 문제는 여행의 각 부분에 대한 기하학을 찾고 있으며 OneMap의 RouteTask에서 돌아온 결과로 그 점을 직접 알 수는 없다고 생각합니다.

전체 경로에서 형상은

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

에 있고 개별 세그먼트는 값 ESRI의 재미 압축 형식 중 하나에 : 일부 문서 및 C# 코드는이를 위해있다

routeResults.results.directions[x].features[y].compressedGeometry 

여기에서 압축 된 형식 :

http://resources.esri.com/help/9.3/arcgisengine/ArcObjects/esrinetworkanalyst/INACompactStreetDirection_CompressedGeometry.htm

정말 개별 세그먼트의 지오메트리가 필요한 경우 C# 코드를 JS로 이식하는 것이 상대적으로 쉽습니다.

OneMap에는 RouteTask에서 결과를 처리하는 방법을 보여주는 완전한 작동 예제 here이 있지만 불행히도 compressedGeometry 필드를 추출하지 않습니다.


편집 : C#을/자바/파이썬 예제와 함께 ESRI here에서 더 많은 샘플 코드.

+0

하지만이 문제를 해결할 생각이 있습니까? –

+0

이 문제를 해결하는 코드와 같습니다. 필자가 제공 한 문서에서 compressedGeometry 필드를 추출하는 방법을 알지 못합니다. –

+0

기회가 생길 때 C# 코드를 이식 할 때마다 문제가 생겨서 내 대답을 업데이트 할 것입니다. – Juffy