<!DOCTYPE>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>旅行商分析</title>
<script src='../libs/SuperMap.Include.js'></script>
<script type="text/javascript">
// var host = document.location.toString().match(/file:\/\//)?"http://localhost:8090":'http://' + document.location.host;
var local, map, layer, vectorLayer, markerLayer,
drawPoint, nodeArray = [], pathTime, i = 0, j = 0, result,
style = {
strokeColor: "#304DBE",
strokeWidth: 3,
pointerEvents: "visiblePainted",
fill: false
},
url1 = "http://localhost:8090/iserver/services/map-China400/rest/maps/tangshan",
url2 = "http://localhost:8090/iserver/services/transportationAnalyst-China400/rest/networkanalyst/BuildNetwork@China400";
function init() {
vectorLayer = new SuperMap.Layer.Vector("Vector Layer");
drawPoint = new SuperMap.Control.DrawFeature(vectorLayer, SuperMap.Handler.Point);
drawPoint.events.on({ "featureadded": drawCompleted });
map = new SuperMap.Map("map", { controls: [
new SuperMap.Control.LayerSwitcher(),
new SuperMap.Control.ScaleLine(),
new SuperMap.Control.Zoom(),
new SuperMap.Control.Navigation({
dragPanOptions: {
enableKinetic: true
}
}),
drawPoint], units: "m"
});
layer = new SuperMap.Layer.TiledDynamicRESTLayer("tangshanlayer", url1, { transparent: true, cacheEnabled: true }, { maxResolution: "auto" });
layer.events.on({ "layerInitialized": addLayer });
vectorLayer = new SuperMap.Layer.Vector("Vector Layer");
markerLayer = new SuperMap.Layer.Markers("Markers");
}
function addLayer() {
map.addLayers([layer, vectorLayer, markerLayer]);
map.setCenter(new SuperMap.LonLat(13207243.52, 4823067.45), 1);
}
function selectPoints() {
clearElements();
drawPoint.activate();
}
function drawCompleted(drawGeometryArgs) {
var point = drawGeometryArgs.feature.geometry,
size = new SuperMap.Size(44, 33),
offset = new SuperMap.Pixel(-(size.w / 2), -size.h),
icon = new SuperMap.Icon("../theme/images/marker.png", size, offset);
markerLayer.addMarker(new SuperMap.Marker(new SuperMap.LonLat(point.x, point.y), icon));
nodeArray.push(point);
}
function findTSPPaths() {
drawPoint.deactivate();
var findTSPPathsService, parameter, analystParameter, resultSetting;
resultSetting = new SuperMap.REST.TransportationAnalystResultSetting({
returnEdgeFeatures: true,
returnEdgeGeometry: true,
returnEdgeIDs: true,
returnNodeFeatures: true,
returnNodeGeometry: true,
returnNodeIDs: true,
returnPathGuides: true,
returnRoutes: true
});
analystParameter = new SuperMap.REST.TransportationAnalystParameter({
resultSetting: resultSetting
// weightFieldName: "length"
});
parameter = new SuperMap.REST.FindTSPPathsParameters({
isAnalyzeById: false,
nodes: nodeArray,
endNodeAssigned: false,
parameter: analystParameter
});
if (nodeArray.length <= 1) {
alert("站点数目有误");
}
findTSPPathsService = new SuperMap.REST.FindTSPPathsService(url2, {
eventListeners: { "processCompleted": processComplete, "processFailed": processFail }
});
findTSPPathsService.processAsync(parameter);
alert(1111);
}
function processComplete(findTSPPathsEventArgs) {
result = findTSPPathsEventArgs.result;
allScheme(result);
}
function processFail(findTSPPathsEventArgs) {
alert("processFail");
console.log(findTSPPathsEventArgs);
}
function allScheme(result) {
console.log(result.tspPathList);
if (i < result.tspPathList.length) {
addPath(result);
} else {
i = 0;
}
}
//以动画效果显示分析结果
function addPath(result) {
if (j < result.tspPathList[i].route.components[0].components.length) {
var pathFeature = new SuperMap.Feature.Vector();
var points = [];
for (var k = 0; k < 2; k++) {
if (result.tspPathList[i].route.components[0].components[j + k]) {
points.push(new SuperMap.Geometry.Point(result.tspPathList[i].route.components[0].components[j + k].x, result.tspPathList[i].route.components[0].components[j + k].y));
}
}
var curLine = new SuperMap.Geometry.LinearRing(points);
pathFeature.geometry = curLine;
pathFeature.style = style;
vectorLayer.addFeatures(pathFeature);
//每隔0.01毫秒加载一条弧段
pathTime = setTimeout(function () { addPath(result); }, 0.01);
j++;
} else {
clearTimeout(pathTime);
j = 0;
i++;
allScheme(result);
}
}
function clearElements() {
nodeArray = [];
i = 0;
j = 0;
markerLayer.clearMarkers();
vectorLayer.removeAllFeatures();
}
</script>
</head>
<body onload="init()">
<div id="toolbar">
<input type="button" class="btn" value="选择站点" onclick="selectPoints()" />
<input type="button" class="btn" value="提交" onclick="findTSPPaths()" />
<input type="button" class="btn" value="清除" onclick="clearElements()" />
</div>
<div id="map"></div>
</body>
</html>