本篇使用与最短路径分析相同用的服务
var closestFacilityTask = new ClosestFacilityTask("http://localhost:6080/arcgis/rest/services/webgis/fzkt_wlfx_all/NAServer/最近设施点");
var params = new ClosestFacilityParameters();
//单位
params.impedenceAttribute = "Miles";
params.defaultCutoff = 7.0;
//不返回事件信息
params.returnIncidents = false;
//返回路径
params.returnRoutes = true;
//路径有参数
params.returnDirections = true;
//服务点
params.facilities = new FeatureSet();
//事件点
params.incidents = new FeatureSet();
//点障碍
params.pointBarriers = new FeatureSet();
//空间参考
params.outSpatialReference = map.SpatialReference;
基本架构与最短路径一样,此处直接贴代码,不懂详见最短路径分析
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
<title>Closest Facilitiestitle>
<link rel="stylesheet" href="https://js.arcgis.com/3.28/esri/css/esri.css" />
<script src="https://js.arcgis.com/3.28/">script>
<style type="text/css">
#map {
width: 100%;
height: 600px;
border: 1px solid #000;
}
.MapClass {
width: 100%;
height: 600px;
border: 1px solid #000;
}
style>
<script src="https://code.jquery.com/jquery-1.12.4.min.js">script>
<script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.js">script>
head>
<body>
<div id="map">div>
<input id="server" type="button" value="服务点" />
<input id="eventPoint" type="button" value="事件点" />
<input id="barriers" type="button" value="障碍点" />
<input id="analyse" type="button" value="分析" />
<script>
require([
"dojo/on",
"dojo/dom",
"dojo/_base/array",
"esri/Color",
"dojo/parser",
"dijit/registry",
"esri/urlUtils",
"esri/map",
"esri/lang",
"esri/graphic",
"esri/InfoTemplate",
"esri/layers/GraphicsLayer",
"esri/renderers/SimpleRenderer",
"esri/layers/ArcGISDynamicMapServiceLayer",
"esri/geometry/Point",
"esri/tasks/FeatureSet",
"esri/tasks/ClosestFacilityTask",
"esri/tasks/ClosestFacilityParameters",
"esri/symbols/SimpleMarkerSymbol",
"esri/symbols/SimpleLineSymbol",
"esri/symbols/TextSymbol",
"dijit/form/ComboBox",
"dijit/layout/BorderContainer",
"dijit/layout/ContentPane"
], function (
on, dom, array, Color, parser, registry,
urlUtils, Map, esriLang, Graphic, InfoTemplate, GraphicsLayer, SimpleRenderer, ArcGISDynamicMapServiceLayer,
Point, FeatureSet,
ClosestFacilityTask, ClosestFacilityParameters,
SimpleMarkerSymbol, SimpleLineSymbol, TextSymbol
) {
var incidentsGraphicsLayer, routeGraphicLayer, closestFacilityTask;
var map = new Map("map");
var layer = new ArcGISDynamicMapServiceLayer("http://localhost:6080/arcgis/rest/services/webgis/fzkt_wlfx_all/MapServer");
map.addLayer(layer)
closestFacilityTask = new ClosestFacilityTask("http://localhost:6080/arcgis/rest/services/webgis/fzkt_wlfx_all/NAServer/最近设施点");
//临近设施分析参数
var params = new ClosestFacilityParameters();
//单位
params.impedenceAttribute = "Miles";
params.defaultCutoff = 7.0;
params.returnIncidents = false;
params.returnRoutes = true;
params.returnDirections = true;
//服务点
params.facilities = new FeatureSet();
//事件点
params.incidents = new FeatureSet();
//点障碍
params.pointBarriers = new FeatureSet();
params.outSpatialReference = map.SpatialReference;
//服务点符号
var facilityPointSymbol = new SimpleMarkerSymbol(
SimpleMarkerSymbol.STYLE_SQUARE,
20,
new SimpleLineSymbol(
SimpleLineSymbol.STYLE_SOLID,
new Color([89, 95, 35]), 2
),
new Color([130, 159, 83, 0.40])
);
//事件点符号
var incidentPointSymbol = new SimpleMarkerSymbol(
SimpleMarkerSymbol.STYLE_CIRCLE,
16,
new SimpleLineSymbol(
SimpleLineSymbol.STYLE_SOLID,
new Color([89, 95, 35]), 2
),
new Color([130, 159, 83, 0.40])
);
//障碍点符号
var barrierSymbol = new SimpleMarkerSymbol();
barrierSymbol.style = SimpleMarkerSymbol.STYLE_X;
barrierSymbol.setSize(12);
barrierSymbol.setColor(new Color("#f1a340"));
incidentsGraphicsLayer = new GraphicsLayer();
//路径线符号
var routePolylineSymbol = new SimpleLineSymbol(
SimpleLineSymbol.STYLE_SOLID,
new Color("#0078df"),
4.0
);
var selectPointID;
//服务点
$("#server").click(function () {
selectPointID = 1;
});
//事件点
$("#eventPoint").click(function () {
selectPointID = 2;
});
//障碍点
$("#barriers").click(function () {
selectPointID = 3;
});
on(map, "mouse-down", function (evt) {
switch (selectPointID) {
case 0:
break;
case 1:
var pointServer = evt.mapPoint;
var gr = new Graphic(pointServer, facilityPointSymbol);
params.facilities.features.push(gr);
break;
case 2:
var pointEvent = evt.mapPoint;
var gr = new Graphic(pointEvent, incidentPointSymbol);
params.incidents.features.push(gr);
break;
case 3:
var pointBarrier = evt.mapPoint;
var gr = new Graphic(pointBarrier, barrierSymbol);
params.pointBarriers.features.push(gr);
break;
}
if (selectPointID != 0) {
addTextPoint("服务点", pointServer, facilityPointSymbol);
addTextPoint("事件点", pointEvent, incidentPointSymbol);
addTextPoint("障碍点", pointBarrier, barrierSymbol);
}
});
function addTextPoint(text, point, symbol) {
var textSymbol = new TextSymbol(text);
textSymbol.setColor(new Color([128, 0, 0]));
var graphicText = Graphic(point, textSymbol);
var graphicpoint = new Graphic(point, symbol);
map.graphics.add(graphicpoint);
map.graphics.add(graphicText);
}
$("#analyse").click(function () {
selectPointID = 0;
if (params.facilities.features.length == 0 || params.incidents.features.length == 0) {
alert("无法分析");
return;
}
closestFacilityTask.solve(params, showRoute)
});
function showRoute(solveResult) {
var routeResults = solveResult.routes;
var res = routeResults.length;
if (res > 0) {
for (var i = 0; i < res; i++) {
var graphicroute = routeResults[i];
var graphic = graphicroute;
graphic.setSymbol(routePolylineSymbol);
map.graphics.add(graphic);
}
}
else {
alert("NULL");
}
}
});
script>
body>
html>