分析---网络分析(临近设施分析)附完整代码

效果图

本篇使用与最短路径分析相同用的服务

与最短路径的不同

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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;

基本架构与最短路径一样,此处直接贴代码,不懂详见最短路径分析

完整代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<!DOCTYPE html>
<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 Facilities</title>

<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>