功能介绍
等时圈是指从某点出发,以某种交通方式在特定时间内能到达的距离覆盖的范围。之前在简书上看到过一篇文章,介绍的是基于mapboxgl使用turf去实现等时圈的方法:等时圈实现 。
最近在使用超图产品进行学习,看到iClient产品里clasic,for Leaflet,for OpenLayers都有服务区分析这个功能,看了一下是和等时圈的功能是一样的,在mapbox里找了一下并没有在分析中找到这个的范例,但是是有接口存在的,就自己尝试实现了这个功能。
数据准备
这个demo中的数据是用的自己的一份数据,坐标系是4326的,自己定义了两个点去做显示,然后根据这两个点作为服务区分析的中心点,自定义权重值实现服务区分析功能。
代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title data-i18n="resources.title_editFeature"></title>
<style>
.editPane {
position: absolute;
right: 10px;
top: 10px;
text-align: center;
background: #FFF;
z-index: 1000;
}
</style>
</head>
<body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0;">
<div id="map" style="margin:0 auto;width: 100%;height: 100%"></div>
<div>
<div class="panel panel-primary editPane" id="editPane">
<div class='panel-heading'>
<h5 class='panel-title text-center'></h5></div>
<div class='panel-body content'>
<input type='button' class='btn btn-default' value="添加点" onclick='addPoint()'/>
<input type='button' class='btn btn-default' value="提交" onclick='commit()'/>
</div>
</div>
</div>
<script type="text/javascript" include="bootstrap,widgets.alert" src="../js/include-web.js"></script>
<script type="text/javascript" src="../../dist/mapboxgl/include-mapboxgl.js"></script>
<script>
var map, id = [], pointFeature, alertDiv, featureService,
url2 = "http:// /rest/networkanalyst/data@data";
//因为是涉密的数据所以就把url里相关的信息都删掉了
var attribution = "<a href='https://www.mapbox.com/about/maps/' target='_blank'>© Mapbox </a>" +
" with <span>© <a href='http://iclient.supermap.io' target='_blank'>SuperMap iClient</a> | </span>" +
" Map Data <span>© <a href='http://support.supermap.com.cn/product/iServer.aspx' target='_blank'>SuperMap iServer</a></span> ";
map = new mapboxgl.Map({
container: 'map', // container id
style: {
"version": 8,
"sources": {
"raster-tiles": {
"attribution": attribution,
"type": "raster",
"tiles": ['http:// /rest/maps/map/zxyTileImage.png?prjCoordSys={"epsgCode":3857}&z={z}&x={x}&y={y}'],
"tileSize": 256
}
},
"layers": [{
"id": "simple-tiles",
"type": "raster",
"source": "raster-tiles",
"minzoom": 5,
"maxzoom": 22
}]
},
center: [116.46 , 40.25 ],
zoom:7 // starting zoom
});
map.addControl(new mapboxgl.NavigationControl(), 'top-left');
map.addControl(new mapboxgl.supermap.LogoControl(), 'bottom-right');
var findServiceAreasService, parameter, analystParameter, resultSetting;
var geojsonPoints = {
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"properties": {
"name": "Point1"
},
"geometry": {
"type": "Point",
"coordinates": [116.39558557938878
, 40.221582584846047]
}
},
{
"type": "Feature",
"properties": {
"name": "Point2"
},
"geometry": {
"type": "Point",
"coordinates": [116.27004461982942
, 40.173681906076759 ]
}
}
]
};
function addPoint(){
//添加分析的显示中心点
map.addSource('geodata', { type: 'geojson', data: geojsonPoints });
map.addLayer({
'id': 'pointlayer',
'type': 'circle',
'source': "geodata",
"paint": {
"circle-radius":5,
"circle-color": "#FFFF00",
"circle-opacity": 1,
"circle-stroke-width": 5,
"circle-stroke-color": "#007cbf",
"circle-stroke-opacity":1
}
});
}
var geo,feature1,feature2;
//只是测试用,所以就只定义了两个点,选取的是两个比较密集区域的节点
function commit() {
var findServiceAreasService, parameter, analystParameter, resultSetting;
var centersArray =[];
var point1 = new SuperMap.Geometry.Point(116.39558557938878
, 40.221582584846047);
var point2 = new SuperMap.Geometry.Point(116.27004461982942
, 40.173681906076759);
//定义两个分析的点
var weightsArray = Array.of(1000,500);
centersArray.push(point1);
centersArray.push(point2);
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: "smLength"
});
parameter = new SuperMap.FindServiceAreasParameters({
centers: centersArray,
isAnalyzeById: false,
parameter: analystParameter,
weights: weightsArray
});
findServiceAreasService = new mapboxgl.supermap.NetworkAnalystService(url2)
.findServiceAreas(parameter,requestCallback);
}
var requestCallback = function processCompleted(findServiceAreasEventArgs) {
//回调函数,处理返回的feature,并通过addP()加载在地图中
var result = findServiceAreasEventArgs.result,
features = [];
var arr1 = findServiceAreasEventArgs.result.serviceAreaList[0].serviceRegion;
var arr2 = findServiceAreasEventArgs.result.serviceAreaList[1].serviceRegion;
feature1 = new SuperMap.Feature.Vector();
feature1.geometry = arr1.geometry;
feature2 = new SuperMap.Feature.Vector();
feature2.geometry = arr2.geometry;
features.push(feature1);
features.push(feature2);
console.log(features);
geo = {
"type": "FeatureCollection",
"features": features
};
addP();
}
function addP(){
//执行添加分析结果
map.addSource('geodata1', { type: 'geojson', data: geo });
map.addLayer({
'id': 'polayer',
'type': 'fill',
'source': "geodata1",
"paint": {
"fill-color": "red",
"fill-opacity": 0.5
}
}
);
}
</script>
</body>
</html>
最终结果
总结
一开始使用的是3857的数据,但是发现需要对传入的数值做坐标转换所以相对来说比较麻烦,就尝试了用4326的数据,总体来说效果没有用mapboxgl和turf去做效果更好,毕竟人家是官方出的效果,从直观视觉上来说有一个颜色的渐变更美观,但是实际的效果、最终的目的还是一致的,就是显示某段时间内可以到达的距离。
附上mapbox官方的效果图,还是挺好看的