目录
Openlayers 矢量元素整体拖动
效果截图:
1. 模板部分
<template>
<div>
<div ref="map" class="map"></div>
</div>
</template>
在模板部分,我们定义了一个包含地图容器的div
元素。
2. 脚本部分
<script>
// 导入所需的OpenLayers库和组件
import "ol/ol.css";
import Map from "ol/Map";
import View from "ol/View";
import TileLayer from "ol/layer/Tile";
import XYZ from "ol/source/XYZ";
import VectorLayer from "ol/layer/Vector";
import VectorSource from "ol/source/Vector";
import Feature from "ol/Feature";
import Point from "ol/geom/Point";
import LineString from "ol/geom/LineString";
import Polygon from "ol/geom/Polygon";
import { fromLonLat } from "ol/proj";
import { Circle, Fill, Stroke, Style } from "ol/style";
import Translate from "ol/interaction/Translate";
export default {
name: "OlMap",
mounted() {
// 创建地图
this.createMap();
},
methods: {
createMap() {
// 创建ArcGIS World Street Map图层
const arcGISLayer = new TileLayer({
source: new XYZ({
url: "https://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/tile/{z}/{y}/{x}"
})
});
// 创建地图容器
const map = new Map({
target: this.$refs.map,
layers: [arcGISLayer],
view: new View({
center: fromLonLat([120, 30]),
zoom: 8
})
});
// 绘制点、线、面要素
const pointFeature = new Feature({
geometry: new Point(fromLonLat([120, 30])),
name: "Point"
});
const lineFeature = new Feature({
geometry: new LineString([fromLonLat([119, 29]), fromLonLat([120, 31])]),
name: "Line"
});
const polygonFeature = new Feature({
geometry: new Polygon([
[
fromLonLat([121, 29]),
fromLonLat([122, 29]),
fromLonLat([122, 30]),
fromLonLat([121, 30]),
fromLonLat([121, 29])
]
]),
name: "Polygon"
});
// 样式设置
const styleFunction = feature => {
const geometryType = feature.getGeometry().getType();
const styles = {
Point: new Style({
image: new Circle({
radius: 10,
fill: new Fill({
color: "rgba(255, 0, 0, 0.6)"
})
})
}),
LineString: new Style({
stroke: new Stroke({
color: "rgba(0, 0, 255, 0.6)",
width: 5
})
}),
Polygon: new Style({
fill: new Fill({
color: "rgba(0, 255, 0, 0.4)"
}),
stroke: new Stroke({
color: "rgba(0, 255, 0, 1)",
width: 3
})
})
};
return styles[geometryType];
};
// 创建矢量图层
const vectorLayer = new VectorLayer({
source: new VectorSource({
features: [pointFeature, lineFeature, polygonFeature]
}),
style: styleFunction
});
// 将矢量图层添加到地图上
map.addLayer(vectorLayer);
// 添加Translate互动选项以实现拖动功能
const translate = new Translate({
layers: [vectorLayer]
});
map.addInteraction(translate);
}
}
};
</script>
在脚本部分,我们首先导入所需的OpenLayers库和组件。然后,在mounted
生命周期钩子中调用createMap
方法来创建地图。
在createMap
方法中,我们首先创建了一个ArcGIS World Street Map图层,并指定了其URL。然后,我们创建了一个地图容器,并将ArcGIS图层添加到地图中。我们还设置了地图的中心点和缩放级别。
接下来,我们创建了点、线和面要素。对于点要素,我们使用Point
类并将其坐标转换为地图坐标系。对于线和面要素,我们使用LineString
和Polygon
类,并将其坐标转换为地图坐标系。
然后,我们定义了一个样式函数styleFunction
,它根据要素的几何类型返回相应的样式。对于点要素,我们使用红色圆形标记。对于线要素,我们使用蓝色线条。对于面要素,我们使用绿色填充和边界。
接下来,我们创建了一个矢量图层,并将之前创建的点、线和面要素添加到矢量图层的数据源中。我们还将样式函数应用到矢量图层中的要素上。
最后,我们创建了一个Translate
交互实例,并将矢量图层作为参数传递给它。然后,我们将交互实例添加到地图上,以实现拖动功能。
3. 样式部分
<style scoped>
.map {
width: 100vw;
height: 100vh;
margin: 0;
padding: 0;
}
</style>
在样式部分,我们使用CSS样式将地图容器的宽度和高度设置为100%以充满整个视口。
4.完整代码
<template>
<div>
<div ref="map" class="map"></div>
</div>
</template>
<script>
import "ol/ol.css";
import Map from "ol/Map";
import View from "ol/View";
import TileLayer from "ol/layer/Tile";
import OSM from "ol/source/OSM";
import VectorLayer from "ol/layer/Vector";
import VectorSource from "ol/source/Vector";
import Feature from "ol/Feature";
import Point from "ol/geom/Point";
import LineString from "ol/geom/LineString";
import Polygon from "ol/geom/Polygon";
import { fromLonLat } from "ol/proj";
import { Circle, Fill, Stroke, Style } from "ol/style";
import XYZ from "ol/source/XYZ";
import Translate from "ol/interaction/Translate";
import Collection from "ol/Collection";
export default {
name: "OlMap",
mounted() {
// 创建地图
this.createMap();
},
methods: {
createMap() {
// 创建ArcGIS World Street Map图层
const arcGISLayer = new TileLayer({
source: new XYZ({
url: "https://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/tile/{z}/{y}/{x}"
})
});
// 创建地图容器
const map = new Map({
target: this.$refs.map,
layers: [arcGISLayer],
view: new View({
center: fromLonLat([120, 30]),
zoom: 8
})
});
// 绘制点、线、面要素
const pointFeature = new Feature({
geometry: new Point(fromLonLat([120, 30])),
name: "Point"
});
const lineFeature = new Feature({
geometry: new LineString([fromLonLat([119, 29]), fromLonLat([120, 31])]),
name: "Line"
});
const polygonFeature = new Feature({
geometry: new Polygon([
[
fromLonLat([121, 29]),
fromLonLat([122, 29]),
fromLonLat([122, 30]),
fromLonLat([121, 30]),
fromLonLat([121, 29])
]
]),
name: "Polygon"
});
// 样式设置
const styleFunction = feature => {
const geometryType = feature.getGeometry().getType();
const styles = {
Point: new Style({
image: new Circle({
radius: 10,
fill: new Fill({
color: "rgba(255, 0, 0, 0.6)"
})
})
}),
LineString: new Style({
stroke: new Stroke({
color: "rgba(0, 0, 255, 0.6)",
width: 5
})
}),
Polygon: new Style({
fill: new Fill({
color: "rgba(0, 255, 0, 0.4)"
}),
stroke: new Stroke({
color: "rgba(0, 255, 0, 1)",
width: 3
})
})
};
return styles[geometryType];
};
// 创建矢量图层
const vectorLayer = new VectorLayer({
source: new VectorSource({
features: [pointFeature, lineFeature, polygonFeature]
}),
style: styleFunction
});
// 将矢量图层添加到地图上
map.addLayer(vectorLayer);
// 添加Translate互动选项以实现拖动功能
const translate = new Translate({
layers: [vectorLayer]
});
map.addInteraction(translate);
}
}
};
</script>
<style scoped>
.map {
width: 100vw;
height: 100vh;
margin: 0;
padding: 0;
}
</style>
5. 总结
通过以上代码,我们实现了使用OpenLayers创建地图,并在地图上添加点、线和面要素。我们还使用样式函数为要素设置样式,并通过Translate
交互实现了整体拖动功能。这样,用户可以在地图上拖动要素。