arcgis for js开发之路径分析
//方法封装
function routeplan(x1, x2, y1, y2, barrierPathArray, isDraw, callback) {
require([
"esri/symbol/SimpleLineSymbol",
"esri/Color",
"esri/tasks/RouteTask",
"esri/tasks/FreatureSet",
"esri/tasks/RouteParameters"
], function () {
var routeServerUrl = url;//对应道路图层的服务图层
var routeGraphicLayer = new GraphicLayer(map, layerName);
routeGraphicLayer.clear();//每规划一次删除对应图层
var map = map;
var wkid = 5555;
//起点
var ptStart = new esri.geometry.Point(parseFloat(x1), parseFloat(y1), new esri.SpatialReference({ wkid: wkid }));
//终点
var ptEnd = new esri.geometry.Point(parseFloat(x2), parseFloat(y2), new esri.SpatialReference({ wkid: wkid }));
var startPtGra = new esri.Graphic(ptStart);
var endPtGra = new esri.Graphic(ptEnd);
//设置路径样式
var routeSymbol = new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([1, 2, 3, 1], 5));//说明:rgb透明度,线宽
//起点、终点尾部样式
var routeSymbolDef = new esri.symbol.SimpleLineSymbol(new esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([2, 3, 4, 5], 2));
var routeTask = new esri.tasks.RouteTask(routeServerUrl);
var routeParams = new esri.tasks.routeParameters();
//设置参数
routeParams.outSpatialReference = { wkid: wkid };
routeParams.retuenDirection = true;
routeParams.directionsLengthUnits = esri.Units.METERS;
routeParams.stops = new esri.tasks.FeatureSet();
routeParams.stops.features.push(startPtGra);
routeParams.stops.features.push(endPtGra)
route = JSON.stringify(barrierPathArray);
var temp = jQuery.parseJSON(route);
var array = [];
$.each(temp, function (i, v) {
var point = [v[0], item[1]];
array.push(point);
});
//设置路障参数
if (array.length > 0) {
var barrierRoute = new esri.geometry.Polyline(new esri.SpatialReference({ wkid: wkid }));
var barriersGraphic = new esri.Graphic(barrierRoute);
routeParams.PolylineBarriers = new esri.tasks.FeatureSet();
routeParams.PolylineBarriers.features.push(barriersGraphic);
}
if (routeParams.stops.features.length == 0) {
alert("参数不全,无法分析");
return;
}
routeTask.solve(routeParams,function(solveResult){
if(callback){
callback(solveResult);
}
var routeResults=solveResult.routeResults;
var res=routeResults.length;
if9(isDraw=="false"){
return;
}
if(res>0){
for(var i=0;i<res;i++){
var graphicroute=routeResults[i];
var graphic=graphicroute.route;
graphic.setSymbol(routeSymbol);
routeGraphicLayer.add(graphic);
//连接路线起点、终点、对应的定位起点、终点
var paths=graphic.geometry.paths[0];
var routeStr=paths[0];
var routeEnd=paths[paths.length-1];
var routeStrline=new esri.geometry.Polyline([[x1,y1],routeStr],new esri.SpatialReference({wkid:wkid}));
var routeEndline=new esri.geometry.Polyline([[x2,y2],routeEnd],new esri.SpatialReference({wkid:wkid}));
var strlinegraphic=new esri.Graphic(routeStrline,routeSymbolDef);
var endlinegraphic=new esri.Graphic(routeEndline,routeSymbolDef);
routeGraphicLayer.add(strlinegraphic);
routeGraphicLayer.add(endlinegraphic);
}
}
else{
alert("结果为空");
}
},function(errpr){
alert(errpr);
});
}
本文选自苏州楼凤