您的位置:首页 > 财经 > 金融 > 职业生涯规划大赛心得体会_手工制作花_网络推广代理平台_黄页网站推广

职业生涯规划大赛心得体会_手工制作花_网络推广代理平台_黄页网站推广

2025/1/23 6:59:53 来源:https://blog.csdn.net/HJW_233/article/details/145119145  浏览:    关键词:职业生涯规划大赛心得体会_手工制作花_网络推广代理平台_黄页网站推广
职业生涯规划大赛心得体会_手工制作花_网络推广代理平台_黄页网站推广

目录

一、案例截图

二、安装OpenLayers库

三、安装Element-UI

四、代码实现

4.1、添加一个点

4.2、删除所有点

4.3、根据经纬度删除点

4.4、给点添加点击事件

4.5、完整代码 

五、Gitee源码


一、案例截图

可以新增/删除标记点,点击标记点可以获取到当前标记点的经纬度信息(绑定了点击事件)。

二、安装OpenLayers库

npm install ol

三、安装Element-UI

没安装的看官方文档:Element - The world's most popular Vue UI framework

四、代码实现

在OpenLayers中,Feature(要素)是地图上表示一个空间实体的对象,它包含的类型有: Point(点) LineString(线段) Polygon(多边形)。

VectorLayer是一个用于在地图上渲染矢量数据的层,支持显示点、线和面等几何图形。

4.1、添加一个点

首先新增一个图层,我们所有的点信息都存放在这个图层上,你也可以添加一个点就新增一个图层,看各自需求,然后初始化经纬度变量。

data() {return {map:null,//所有点信息都放在这个图层pointLayer: new VectorLayer({source: new VectorSource(),}),form:{lon:'',lat:'',},}
},

在地图初始化以后要把这个图层添加到地图上:

//把图层添加到地图上
this.map.addLayer(this.pointLayer);

新增一个点JS关键代码:

/*** 根据经纬度坐标添加自定义图标 支持base64*/
addPoints() {let coordinate = [this.form.lon,this.form.lat];// 创建feature要素,一个feature就是一个点坐标信息let feature = new Feature({geometry: new Point(coordinate),});// 设置要素的图标feature.setStyle(new Style({// 设置图片效果image: new Icon({src: 'http://api.tianditu.gov.cn/img/map/markerA.png',// anchor: [0.5, 0.5],scale: 1,}),}),);// 要素添加到地图图层上this.pointLayer.getSource().addFeature(feature);// 设置中心this.map.getView().setCenter(coordinate);
},

4.2、删除所有点

removeAllPoint(){// 从图层源中获取当前要素const features = this.pointLayer.getSource().getFeatures();// 遍历要素features.forEach(feature => {this.pointLayer.getSource().removeFeature(feature);});
}

4.3、根据经纬度删除点

删除一个点JS关键代码:

removePoint() {let coordinate = [this.form.lon,this.form.lat];// 从图层源中获取当前要素const features = this.pointLayer.getSource().getFeatures();let _that = this;// 遍历要素,查找匹配的坐标features.forEach(feature => {const geometry = feature.getGeometry();const featureCoordinates = geometry.getCoordinates();// 检查经纬度是否匹配if (featureCoordinates[0] == coordinate[0] && featureCoordinates[1] == coordinate[1]) {// 找到匹配的要素,移除它_that.pointLayer.getSource().removeFeature(feature);}});
},

4.4、给点添加点击事件

JS关键代码:

initPointEvent(){//给点初始化点击事件this.map.on("singleclick", (e) => {let pixel = this.map.getEventPixel(e.originalEvent);let feature = this.map.forEachFeatureAtPixel(pixel, function(feature) { return feature; });let coordinate = e.coordinate;if(feature){console.log("当前点击经纬度:"+coordinate);}});
}

4.5、完整代码 

<template><div><div style="margin-bottom: 10px"><el-button type="danger" @click="removeAllPoint">删除所有点标记</el-button><el-input v-model="form.lon" placeholder="经度" style="margin-left: 10px;width: 200px"></el-input><el-input v-model="form.lat" placeholder="纬度" style="margin-left: 10px;width: 200px"></el-input><el-button type="success" @click="addPoints" style="margin-left: 10px">新增</el-button><el-button type="warning" @click="removePoint" style="margin-left: 10px">删除</el-button></div><div id="map-container"></div></div></template>
<script>
import { Map, View,Feature } from 'ol'
import { Tile as TileLayer } from 'ol/layer'
import { get } from 'ol/proj';
import { getWidth, getTopLeft } from 'ol/extent'
import { WMTS } from 'ol/source'
import WMTSTileGrid from 'ol/tilegrid/WMTS'
import { defaults as defaultControls} from 'ol/control';
import VectorLayer from "ol/layer/Vector";
import VectorSource from "ol/source/Vector";
import {Point} from "ol/geom";
import {Icon, Style} from "ol/style";export const projection = get("EPSG:4326");
const projectionExtent = projection.getExtent();
const size = getWidth(projectionExtent) / 256;
const resolutions = [];
for (let z = 0; z < 19; ++z) {resolutions[z] = size / Math.pow(2, z);
}export default {data() {return {map:null,//所有点信息都放在这个图层pointLayer: new VectorLayer({source: new VectorSource(),}),form:{lon:'',lat:'',},}},mounted(){this.initMap() // 加载矢量底图},methods:{initMap() {const KEY = '你申请的KEY'this.map = new Map({target: 'map-container',layers: [// 底图new TileLayer({source: new WMTS({url: `http://t{0-6}.tianditu.com/vec_c/wmts?tk=${KEY}`,layer: 'vec', // 矢量底图matrixSet: 'c', // c: 经纬度投影 w: 球面墨卡托投影style: "default",crossOrigin: 'anonymous', // 解决跨域问题 如无该需求可不添加format: "tiles", //请求的图层格式,这里指定为瓦片格式wrapX: true, // 允许地图在 X 方向重复(环绕)tileGrid: new WMTSTileGrid({origin: getTopLeft(projectionExtent),resolutions: resolutions,matrixIds: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15','16','17','18']})})}),// 标注new TileLayer({source: new WMTS({url: `http://t{0-6}.tianditu.com/cva_c/wmts?tk=${KEY}`,layer: 'cva', //矢量注记matrixSet: 'c',style: "default",crossOrigin: 'anonymous',format: "tiles",wrapX: true,tileGrid: new WMTSTileGrid({origin: getTopLeft(projectionExtent),resolutions: resolutions,matrixIds: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15','16','17','18']})})})],view: new View({center: [118.958366,32.119577],projection: projection,zoom: 12,maxZoom: 17,minZoom: 1}),//加载控件到地图容器中controls: defaultControls({zoom: false,rotate: false,attribution: false})})//把图层添加到地图上this.map.addLayer(this.pointLayer);//标记点初始化点击事件this.initPointEvent();},/*** 根据经纬度坐标添加自定义图标 支持base64*/addPoints() {let coordinate = [this.form.lon,this.form.lat];// 创建feature要素,一个feature就是一个点坐标信息let feature = new Feature({geometry: new Point(coordinate),});// 设置要素的图标feature.setStyle(new Style({// 设置图片效果image: new Icon({src: 'http://api.tianditu.gov.cn/img/map/markerA.png',// anchor: [0.5, 0.5],scale: 1,}),}),);// 要素添加到地图图层上this.pointLayer.getSource().addFeature(feature);// 设置中心this.map.getView().setCenter(coordinate);},removePoint() {let coordinate = [this.form.lon,this.form.lat];// 从图层源中获取当前要素const features = this.pointLayer.getSource().getFeatures();let _that = this;// 遍历要素,查找匹配的坐标features.forEach(feature => {const geometry = feature.getGeometry();const featureCoordinates = geometry.getCoordinates();// 检查经纬度是否匹配if (featureCoordinates[0] == coordinate[0] && featureCoordinates[1] == coordinate[1]) {// 找到匹配的要素,移除它_that.pointLayer.getSource().removeFeature(feature);}});},removeAllPoint(){// 从图层源中获取当前要素const features = this.pointLayer.getSource().getFeatures();// 遍历要素features.forEach(feature => {this.pointLayer.getSource().removeFeature(feature);});},initPointEvent(){//给点初始化点击事件this.map.on("singleclick", (e) => {let pixel = this.map.getEventPixel(e.originalEvent);let feature = this.map.forEachFeatureAtPixel(pixel, function(feature) { return feature; });let coordinate = e.coordinate;if(feature){console.log("当前点击经纬度:"+coordinate);}});}}
}
</script>
<style scoped>
#map-container {width: 100%;height: 100vh;
}
</style>

五、Gitee源码

地址:Vue2+OpenLayers添加+删除点+点击事件功能实现

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com