引用css/js
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.14.1/css/ol.css" type="text/css">
<script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.14.1/build/ol.js"></script>
初始化OpenLayers 地图
var map = new ol.Map({
target: 'map',
layers: [nativeOsmLayer],
view: new ol.View({
center: ol.proj.fromLonLat([37.41, 8.82]),
zoom: 4,
minZoom: 10,
maxZoom: 21
})
});
地图瓦片数据源 new ol.layer.Tile
let nativeOsmLayer = new ol.layer.Tile({
source: new ol.source.TileWMS({
url: baseMapUrl,
/* 参数 */
params: {
LAYERS: baseLayer, //此处可以是单个图层名称,也可以是图层组名称,或多个图层名称
TILED: true,
FORMAT: "image/png"
},
serverType: "geoserver" //服务器类型
})
});
创建marker(矢量图层的数据来源 ol.source.Vector )
包含四个事件,addfeature,changefeature,clear,removefeature。
/* 矢量标注的数据源 进行数据操作 eventMarker放入全局对数据进行操作*/
let eventMarker = new ol.source.Vector({
features: []
});
/* 将数据源添加地图上(我们需要一个vector的layer来放置图标)*/
let eventLayer = new ol.layer.Vector({
source: eventMarker,
visible: true, //设置true或者false是否显示 默认true显示
name: "ccc" //设置图层名称,根据名称查找图层
});
this.map.addLayer(eventLayer);
另一种方式 ( 未亲测 ) ```javascript let layer = new ol.layer.Vector({ source: new ol.source.Vector(), })
this.map.addLayer(layer); / 创建一个features,并设置好放在地图上的位置 / let anchor = new ol.features({ geometry: new ol.geom.Point([104,30]) }) anchor.setStyle(new ol.style.Style({ image: new ol.style.Icon({ anchor: [0.5, 30], //锚点 anchorOrigin: “top-right”, //锚点源 anchorXUnits: “fraction”, //锚点X值单位 anchorYUnits: “pixels”, //锚点Y值单位 offsetOrigin: “top-right”, //偏移原点 opacity: 1, scale:1,//图片大小用缩放控制 src:’img/anchor.png’ }), }); ) layer.getSource().addFeature(anchor)
<a name="TmPfi"></a>
#### 创建矢量图层图标 addFeature
```javascript
/*
item-marker数据
type-自定义图层名称
layerName-添加marker到哪个图层
eventOrSingImg-交通事件和交通标志的图片
*/
function creatMarker(item, type, layerName, eventOrSingImg){
let wgs84 = [item.longitude,item.latitude];
let eventFeature = new ol.Feature({
geometry: new ol.geom.Point(translateInMap(wgs84)), //几何信息
layerType:type,
//因为openlayers是setStyle置空图层 那么再次点击需要找到自身图片
imgUrl:eventOrSingImg,
data:item
});
eventFeature.setStyle(createNodeStyle(img_address));
/* 将数据添加到矢量图数据源中 */
eventMarker.addFeature(eventFeature);
}
//****************************坐标点转换为大地坐标系*************************
translateInMap(center) {
return ol.proj.fromLonLat(center);
},
//****************************坐标点转换为wgs84******************************
transFormMap(center) {
let pos = [
parseFloat(center[0]),
parseFloat(center[1])
];
return pos = ol.proj.transform(pos, "EPSG:3857", "EPSG:4326");
},
获取数据源数据设置样式 getFeatures()
let allEventMarker = eventMarker.getFeatures();
for (let i = 0; i < allEventMarker.length; i++) {
var img_address = "1.png";
allEventMarker[i].setStyle(createNodeStyle(img_address,'',0.5))
}
function createNodeStyle(imgurl,scale=1) {
return new ol.style.Style({
image: new ol.style.Icon({
color: 'rgba(255, 184, 0, 0.56)',//图标着色
anchor: [0.5, 30], //锚点
anchorOrigin: "top-right", //锚点源
anchorXUnits: "fraction", //锚点X值单位
anchorYUnits: "pixels", //锚点Y值单位
offsetOrigin: "top-right", //偏移原点
opacity: 1,
scale:1,//图片大小用缩放控制
src:imgurl
}),
});
}
添加控件
// 实例化比例尺控件
let scaleLineControl = new ol.control.ScaleLine({
// 设置比例尺单位为degrees、imperial、us、nautical或metric(度量单位)
units: "metric"
});
map.addControl(scaleLineControl);
地图放大缩小
const view = map.getView();
const zoom = view.getZoom();
view.setZoom(zoom + 1);
view.setZoom(zoom - 1);
添加鼠标右键菜单
<template>
<div
v-show="menuShow"
id="contextmenu_container"
ref="contextmenu"
class="contextmenu"
>
<ul>
<div @click="creatNode">设置事件发生地点</div>
<li v-for="(item,index) in this.addForm.pathPoints">
<div @click="setStart(index)">设置上游起点{{index+1}}</div>
<div @click="setEnd(index)">设置下游终点{{index+1}}</div>
</li>
</ul>
</div>
</template>
<script>
export default {
methods:{
//添加鼠标右键
addMenu_over(){
var self = this;
this.menuShow = true;
/*使用这个 html 元素初始化一个 overlay,并将 overlay 添加到地图中*/
this.menu_overlay= new ol.Overlay({
element: document.getElementById("contextmenu_container"),
positioning: 'right-top'
});
this.map.addOverlay(self.menu_overlay);
$(self.map.getViewport()).unbind('contextmenu').on("contextmenu", function(event){
event.preventDefault();//屏蔽自带的右键事件
var pixel = [event.clientX,event.clientY];
/*获取地图相应的点击坐标*/
var coordinate = self.map.getEventCoordinate(event.originalEvent);
self.coordinate = coordinate;
/*设置 overlay 的显示位置*/
self.menu_overlay.setPosition(coordinate);
});
},
}
}
</script>
右键菜单隐藏
this.menu_overlay.setPosition(undefined);