作者:doremi
随着SuperMap 9D产品的发布,带来了全新的SuperMap iClient 9D产品。今天就为大家介绍下其中的一款SuperMap iClient 9D for OpenLayers的入门用法。
####产品介绍
SuperMap iClient 9D for OpenLayers是一套基于OpenLayers的云GIS网络客户端开发平台,支持访问SuperMap iServer/IExpress/iPortal/iManager/Online的地图、服务和资源,为用户提供了完整专业的GIS能力,同事提供了优秀的可视化功能。
####快速入门
第一步:产品下载并解压
下载SuperMap iClient 9D for OpenLayers源码或者压缩包,解压文件
下载地址:
GitHub托管地址:https://github.com/SuperMap/iClient9/tree/master/src/openlayers
OSChina托管地址:https://gitee.com/isupermap/iClient9/tree/master/src/openlayers
第二步:引用资源文件
需要引用ol.js,iclient9-openlayers.js文件
<script type="text/javascript" src="https://cdn.bootcss.com/openlayers/4.2.0/ol.js"></script>
<script type="text/javascript" src="http://iclient.supermap.io/dist/iclient9-openlayers.js"></script>
第三步:添加承载地图的< div >
<div id="map" style="margin:0 auto;width:100%;height:100%;"></div>
第四步:写入JavaScript代码创建地图
var map = new ol.Map({
target: 'map',
layers: [new ol.layer.Tile({
source: new ol.source.TileSuperMapRest({
url: "http://localhost:8090/iserver/services/map/rest/maps/World"
}),
})]
});
运行效果
这样一个简单的地图我们就创建成功了,可以看出所需的代码量非常的少。接下来给大家介绍一些常用的功能。
####常用控件
在SuperMap iClient 9D for OpenLayers中还有很多非常实用的控件,像鹰眼(OverView),全幅显示(ZoomToExtent),鼠标位置(MousePosition),全屏显示(FullScreen)等。
添加控件有两种方法,第一种是map初始化的时候,在controls属性中直接添加;第二种是使用map.addControl()方法进行添加。现在我们就来进行添加
map = new ol.Map({
target:'map',
controls:ol.control.defaults({attributionOptions:{collapsed:false}}).extend([
new ol.supermap.control.Logo(),
new ol.control.FullScreen(),
new ol.control.ZoomToExtent({
extent:[7494062.9295,119581.2150, 16426799.7795,7995652.6095]
}),
new ol.control.MousePosition({
coordinateFormat:ol.coordinate.createStringXY(4),
projection:'EPSG:3857',
className:'custom-mouse-position',
target:document.getElementById('mouse-position'),
undefinedHTML:' '
})
]),
view:new ol.View({
center:[0,0],
zoom:1,
projection:'EPSG:3857'
})
});
var overView = new ol.control.OverviewMap();
map.addControl(overView);
运行效果:
左上角“E”图标是全幅显示控件,左下角是鹰眼控件,右上角是鼠标位置控件和全屏显示控件。
####添加marker,点击交互
第一步,添加自定义marker
创建feature对象和feature的样式并将样式赋予feature,再将feature添加到vector图层中
var iconFeature = new ol.Feature({
geometry:new ol.geom.Point([0,0]),
name: 'Null Island',
population: 4000,
rainfall: 500
});
var iconStyle = new ol.style.Style({
image: new ol.style.Icon(/** @type {olx.style.IconOptions} */ ({
anchor: [0.5, 46],
anchorXUnits: 'fraction',
anchorYUnits: 'pixels',
src: 'https://openlayers.org/en/v4.6.4/examples/data/icon.png'
}))
});
iconFeature.setStyle(iconStyle);
var vectorSource = new ol.source.Vector({
features: [iconFeature]
});
var vectorLayer = new ol.layer.Vector({
source: vectorSource
});
map.addLayer(vectorLayer);
第二步,创建popup
添加popup的DOM对象,定义它的CSS样式,添加承载popup的overlay图层
<div id="popup" class="ol-popup">
<a href="#" target="_blank" rel="external nofollow" id="popup-closer" class="ol-popup-closer"></a>
<div id="popup-content"></div>
</div>
var container = document.getElementById('popup');
var content = document.getElementById('popup-content');
var closer = document.getElementById('popup-closer');
var overlay = new ol.Overlay({
element: container,
autoPan: true,
autoPanAnimation: {
duration: 250
}
});
map = new ol.Map({
target:'map',
view:new ol.View({
center:[0,0],
zoom:2,
projection:'EPSG:4326'
}),
overlays:[overlay]
});
第三步,添加选择控件
添加ol,interaction.Select()控件,并注册点击事件
var select = new ol.interaction.Select();
map.addInteraction(select);
select.on('select',function(e){
var feature = e.selected[0];
content.innerHTML = 'I am a custom symbol';
overlay.setPosition(feature.getGeometry().getCoordinates());
});
运行代码,我们自定义的marker就成功添加到地图中了,并且能够点击交互,弹出气泡框。
要素绘制
SuperMap iClient 9D for OpenLayers给我们提供了非常方便的要素绘制控件,我们可以在vector图层上画出任意我们想要的点线面对象。
调用ol.interaction.Draw(),通过改变不同的type添加到map控件中使能够在地图上绘制出不同的几何对象。包括点(Point),线(LineString),多边形(Polygon),圆(Circle)
var typeSelect = document.getElementById('type');
function addInteraction() {
var value = typeSelect.value;
if (value !== 'None') {
draw = new ol.interaction.Draw({
source: source,
type: typeSelect.value
});
map.addInteraction(draw);
}
}
typeSelect.onchange = function() {
map.removeInteraction(draw);
addInteraction();
};
addInteraction();
以上的话就是SuperMap iClient 9D for OpenLayers的入门介绍,想了解更多的API介绍和炫酷Demo可以访问我们的SuperMap iClient 9D官网http://icltest.supermapol.com/