开发工具与关键技术:SuperMap iDesktop9D、iServer、Visual Studio 2015、Sql、.Net、C#
撰写时间:2019年01月19日
最佳路径分析:选点提交分析最佳路径功能,以下为功能完成图
先到SuperMap iDesktop 9D 此数据源里构建二位网格选择所有线数据,再选择此二位网格数据进行线拓扑处理,处理完成之后就可以进行检测,点击此创建处理好的二维网格点击SuperMap iDesktop 9D里的空间分析下的最佳路径进行检测,选择站点后运行路线显示即成功。
见实现的代码:
先声明图层参数、图层样式用于图层显示,在这里的连接和后面提交方法的权重名称可以到iServer的服务列表下的交通网络分析里进行复制
//街道网格
var url2 = "http://localhost:8090/iserver/services/transportationAnalyst-BADistrictWorkingSpace/rest/networkanalyst/Xianlu@ShenZhenBaoAn_Data";
var drawPoint, vctorLayer, markerLayers, select, nodeArray = [], pathTime, pathListIndex = 0, routeCompsIndex = 0,
style = {
strokeColor: "#304DBE",
strokeWidth: 3,
pointerEvents: "visiblePainted",
fill: false
},
styleGuidePoint = {
pointRadius: 10,
externalGraphic: "/ContentTGM/Maps/images/walk.png"
},
styleGuideLine = {
strokeColor: "#25FF25",
strokeWidth: 6,
fill: false
};
vectorLayer = new SuperMap.Layer.Vector("路线", {displayInLayerSwitcher:false});
drawPoint = new SuperMap.Control.DrawFeature(vectorLayer, SuperMap.Handler.Point);
select = new SuperMap.Control.SelectFeature(vectorLayer, { onSelect: onFeatureSelects, onUnselect: onFeatureUnselects });
drawPoint.events.on({ "featureadded": drawCompleted });
markerLayers = new SuperMap.Layer.Markers("站点",{displayInLayerSwitcher:false});
记得把图层的参数填进添加图层方法里,接下来具体方法:
选择站点:
//选择站点
function selectPoints() {
clearElements();
drawPoint.activate();
}
//接收站点的信息并添加站点的icon样式
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("/SuperMap/theme/images/marker.png", size, offset);
markerLayers.addMarker(new SuperMap.Marker(new SuperMap.LonLat(point.x, point.y), icon));
nodeArray.push(point);
}
//选中时显示路径指引信息
function onFeatureSelects(feature) {
if (feature.attributes.description) {
popup = new SuperMap.Popup("chicken",
feature.geometry.getBounds().getCenterLonLat(),
new SuperMap.Size(200, 30),
"<div style='font-size:.8em; opacity: 0.8'>" + feature.attributes.description + "</div>",
null, false);
feature.popup = popup;
map.addPopup(popup);
}
if (feature.geometry.CLASS_NAME != "SuperMap.Geometry.Point") {
feature.style = styleGuideLine;
vectorLayer.redraw();
}
}
//清除要素时调用此函数
function onFeatureUnselects(feature) {
map.removePopup(feature.popup);
feature.popup.destroy();
feature.popup = null;
if (feature.geometry.CLASS_NAME != "SuperMap.Geometry.Point") {
feature.style = style;
}
vectorLayer.redraw();
}
进行提交:
//提交方法
function findPath() {
drawPoint.deactivate();
var findPathService, 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: "SmLength"//这里是权重名称
});
parameter = new SuperMap.REST.FindPathParameters({
isAnalyzeById: false,
nodes: nodeArray,
hasLeastEdgeCount: false,
parameter: analystParameter
});
if (nodeArray.length <= 1) {
alert("站点数目有误");
}
findPathService = new SuperMap.REST.FindPathService(url2, {
eventListeners: { "processCompleted": processCompleted }
});
findPathService.processAsync(parameter);
}
function processCompleted(findPathEventArgs) {
var result = findPathEventArgs.result;
allScheme(result);
}
function allScheme(result) {
if (pathListIndex < result.pathList.length) {
addPath(result);
} else {
pathListIndex = 0;
//线绘制完成后会绘制关于路径指引点的信息
addPathGuideItems(result);
}
}
//以动画效果显示分析结果
function addPath(result) {
if (routeCompsIndex < result.pathList[pathListIndex].route.components.length) {
var pathFeature = new SuperMap.Feature.Vector();
var points = [];
for (var k = 0; k < 2; k++) {
if (result.pathList[pathListIndex].route.components[routeCompsIndex + k]) {
points.push(new SuperMap.Geometry.Point(result.pathList[pathListIndex].route.components[routeCompsIndex + k].x, result.pathList[pathListIndex].route.components[routeCompsIndex + k].y));
}
}
var curLine = new SuperMap.Geometry.LinearRing(points);
pathFeature.geometry = curLine;
pathFeature.style = style;
vectorLayer.addFeatures(pathFeature);
//每隔0.001毫秒加载一条弧段
pathTime = setTimeout(function () { addPath(result); }, 0.001);
routeCompsIndex++;
} else {
clearTimeout(pathTime);
routeCompsIndex = 0;
pathListIndex++;
allScheme(result);
}
}
function addPathGuideItems(result) {
vectorLayer.removeAllFeatures();
//显示每个pathGuideItem和对应的描述信息
for (var k = 0; k < result.pathList.length; k++) {
var pathGuideItems = result.pathList[pathListIndex].pathGuideItems, len = pathGuideItems.length;
for (var m = 0; m < len; m++) {
var guideFeature = new SuperMap.Feature.Vector();
guideFeature.geometry = pathGuideItems[m].geometry;
guideFeature.attributes = { description: pathGuideItems[m].description };
if (guideFeature.geometry.CLASS_NAME === "SuperMap.Geometry.Point") {
guideFeature.style = styleGuidePoint;
}
else {
guideFeature.style = style;
}
vectorLayer.addFeatures(guideFeature);
}
}
select.activate();
}
//清除
function clearElements() {
pathListIndex = 0;
routeCompsIndex = 0;
nodeArray = [];
select.deactivate();
if (vectorLayer.selectedFeatures.length > 0) {
map.removePopup(vectorLayer.selectedFeatures[0].popup);
}
vectorLayer.removeAllFeatures();
markerLayers.clearMarkers();
}
在这里主要是在前面把二维网格数据集弄好,后面的就简单了,都可以在SuperMap iServer的for JavaScript示例里找到。