本文主要是介绍iClient旅行商分析模拟物资配送,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
作者:lly
背景
在特殊时期,需要根据情况对交通进行管控,实行某些道路区域禁止通行的措施,在这中情况下,需要根据具体禁行情况进行物资配送的规划,所以我们使用iClient旅行商分析,来进行物资配送的模拟。
一、构建显示图层
分别构建禁行点,配送中心,目的点图层和数组
var resultLayer = L.featureGroup().addTo(map);var StopLayer = L.featureGroup().addTo(map);var CenterLayer = L.featureGroup().addTo(map);var targetLayer = L.featureGroup().addTo(map);var centerPoint=[];var targetPoint=[];var stopPoint=[];
二、站点添加
监听鼠标事件,添加各点
function addStop(){StopLayer.clearLayers();map.off('click');stopPoint=[];map.on('click', function(e) {L.circleMarker([e.latlng.lat, e.latlng.lng], {color: 'red'}).addTo(StopLayer);stopPoint.push(L.latLng(e.latlng.lat, e.latlng.lng));});}function addTarget(){targetLayer.clearLayers();map.off('click');targetPoint=[];map.on('click', function(e) {L.marker([e.latlng.lat, e.latlng.lng]).addTo(targetLayer);targetPoint.push(L.latLng(e.latlng.lat, e.latlng.lng));});}function addCenter(){CenterLayer.clearLayers();map.off('click');centerPoint=[];map.on('click', function(e) {L.marker([e.latlng.lat, e.latlng.lng],{icon: myIcon}).addTo(CenterLayer);centerPoint.push(L.latLng(e.latlng.lat, e.latlng.lng));});}
三、旅行商分析
判断传入点是否存在
if(centerPoint.length==0){alert("请添加配送中心");return;}else if(targetPoint.length==0){alert("请添加配送目的地");return;}
创建分析实例
findMTSPPathsService = L.supermap.networkAnalystService(serviceUrl);//创建多旅行商分析参数实例resultSetting = new SuperMap.TransportationAnalystResultSetting({returnEdgeFeatures: true,returnEdgeGeometry: true,returnEdgeIDs: true,returnNodeFeatures: true,returnNodeGeometry: true,returnNodeIDs: true,returnPathGuides: true,returnRoutes: true});analystParameter = new SuperMap.TransportationAnalystParameter({resultSetting: resultSetting,weightFieldName: "length",barrierPoints:stopPoint});findMTSPPathsParameter = new SuperMap.FindMTSPPathsParameters({centers:centerPoint,isAnalyzeById: false,nodes: targetPoint,hasLeastTotalCost: true,parameter: analystParameter});
进行路线查找
findMTSPPathsService.findMTSPPaths(findMTSPPathsParameter, function (serviceResult) {var result = serviceResult.result;result.pathList.map(function (result) {L.geoJSON(result.route, {color: "green"}).addTo(resultLayer);});});
四、结果展示
.
完整代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title data-i18n="resources.title_findMTSPPathsServiceLogistics"></title>
<script type="text/javascript" src="../js/include-web.js"></script>
</head>
<body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0;"><button onclick="addStop()">防控禁行点</button><button onclick="addCenter()">添加配送中心</button><button onclick="addTarget()">添加目的点</button><button onclick="findMTSPPathsProcess()">模拟配送</button>
<div id="map" style="margin:0 auto;width: 100%;height: 100%"></div>
<script type="text/javascript" src="../../dist/leaflet/include-leaflet.js"></script>
<script type="text/javascript">var host = window.isLocal ? window.server : "https://iserver.supermap.io";var map, findMTSPPathsService, resultSetting, analystParameter, findMTSPPathsParameter,url="http://localhost:8090/iserver/services/data-changchun/rest/data",baseUrl = host + "/iserver/services/map-changchun/rest/maps/长春市区图",serviceUrl = host + "/iserver/services/transportationanalyst-sample/rest/networkanalyst/RoadNet@Changchun";map = L.map('map', {crs: L.CRS.NonEarthCRS({bounds: L.bounds([48.4, -7668.25], [8958.85, -55.58]),origin: L.point(48.4, -55.58)}),center: [-3600, 4700],maxZoom: 18,zoom: 6});L.supermap.tiledMapLayer(baseUrl, {noWrap: true}).addTo(map);var resultLayer = L.featureGroup().addTo(map);var StopLayer = L.featureGroup().addTo(map);var CenterLayer = L.featureGroup().addTo(map);var targetLayer = L.featureGroup().addTo(map);var myIcon = L.icon({iconUrl: '../img/marker.png',iconSize: [44, 30]});var centerPoint=[];var targetPoint=[];var stopPoint=[];function findMTSPPathsProcess() {resultLayer.clearLayers();if(centerPoint.length==0){alert("请添加配送中心");return;}else if(targetPoint.length==0){alert("请添加配送目的地");return;}//创建多旅行商分析服务实例findMTSPPathsService = L.supermap.networkAnalystService(serviceUrl);//创建多旅行商分析参数实例resultSetting = new SuperMap.TransportationAnalystResultSetting({returnEdgeFeatures: true,returnEdgeGeometry: true,returnEdgeIDs: true,returnNodeFeatures: true,returnNodeGeometry: true,returnNodeIDs: true,returnPathGuides: true,returnRoutes: true});analystParameter = new SuperMap.TransportationAnalystParameter({resultSetting: resultSetting,weightFieldName: "length",barrierPoints:stopPoint});findMTSPPathsParameter = new SuperMap.FindMTSPPathsParameters({centers:centerPoint,isAnalyzeById: false,nodes: targetPoint,hasLeastTotalCost: true,parameter: analystParameter});//进行查找findMTSPPathsService.findMTSPPaths(findMTSPPathsParameter, function (serviceResult) {var result = serviceResult.result;result.pathList.map(function (result) {L.geoJSON(result.route, {color: "green"}).addTo(resultLayer);});});}function addStop(){StopLayer.clearLayers();map.off('click');stopPoint=[];map.on('click', function(e) {L.circleMarker([e.latlng.lat, e.latlng.lng], {color: 'red'}).addTo(StopLayer);stopPoint.push(L.latLng(e.latlng.lat, e.latlng.lng));});}function addTarget(){targetLayer.clearLayers();map.off('click');targetPoint=[];map.on('click', function(e) {L.marker([e.latlng.lat, e.latlng.lng]).addTo(targetLayer);targetPoint.push(L.latLng(e.latlng.lat, e.latlng.lng));});}function addCenter(){CenterLayer.clearLayers();map.off('click');centerPoint=[];map.on('click', function(e) {L.marker([e.latlng.lat, e.latlng.lng],{icon: myIcon}).addTo(CenterLayer);centerPoint.push(L.latLng(e.latlng.lat, e.latlng.lng));});}
</script>
</body>
</html>
这篇关于iClient旅行商分析模拟物资配送的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!