Vue+Openlayers实现地图上绘制线

场景

Vue+Openlayers实现显示图片并分优先级多图层加载:

Vue+Openlayers实现显示图片并分优先级多图层加载_BADAO_LIUMANG_QIZHI的博客-CSDN博客

上面是添加多个点的图层,如果添加线的图层,形如下面的效果

注:

博客:
BADAO_LIUMANG_QIZHI的博客_霸道流氓气质_CSDN博客
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

1、导入新的模块

//导入基本模块
import "ol/ol.css";
import Map from "ol/Map";
import View from "ol/View";
import { Point,LineString } from "ol/geom";
import Feature from "ol/Feature";
import { Icon,Style,Stroke} from "ol/style";
//导入相关模块
import { Tile as TileLayer , Vector as VectorLayer } from 'ol/layer'
import { TileWMS ,Vector as VectorSource } from 'ol/source'

2、声明一个线的图层和数据源

  data() {
    return {
      map: null, // map地图
      layer:null, //地图图层
      lightLayer:null, //灯图层
      houseLayer:null, //房子图层
      lineLayer:null, //线图层
      lineSource:null, //线数据源

3、添加要绘制的线的坐标数组

注意这里必须是数组嵌套数组的格式

      //线的数据
      lineData:[
        [986434.4063822062, 215782.0959711917],
        [989701.5290279881,217149.84072807242],
        [990613.3107184113,215946.4192185118],
      ],

这里是三个坐标点,会将这三个点依次连接起来

4、线的图层进行赋值

      //线的图层
      this.lineSource = new VectorSource({ wrapX: false });
      this.lineLayer = new VectorLayer({
          source: this.lineSource,
      });

5、map中加入线的图层

      this.map = new Map({
        //地图容器ID
        target: "map",
        //引入地图
        layers: [this.layer,this.lightLayer,this.houseLayer,this.lineLayer],
        view: new View({
          //地图中心点
          center: [987777.93778, 213834.81024],
          zoom: 12,
          minZoom:6, // 地图缩放最小级别
          maxZoom:19,
        }),
      });

6、页面加载完调用初始化地图的方法,方法中调用画线的方法

    //画线
    drawLine(){
      let pointData = this.lineData; // 所有点位信息
      //下边来添加一线feature
      var feature = new Feature({
          type: "lineStyle",
          geometry: new LineString(
              pointData // 线的坐标
          ),
      });
      let color = 'green';
      let lineStyle = new Style({
          stroke: new Stroke({
              color: color,
              width: 4,
          }),
      });
      // 添加线的样式
      feature.setStyle(lineStyle);
      // 添加线的fature
      this.lineSource.addFeature(feature);
    },

7、完整示例代码

​
<template>
  <div id="map" class="map"></div>
</template>

<script>
//导入基本模块
import "ol/ol.css";
import Map from "ol/Map";
import View from "ol/View";
import { Point,LineString } from "ol/geom";
import Feature from "ol/Feature";
import { Icon,Style,Stroke} from "ol/style";
//导入相关模块
import { Tile as TileLayer , Vector as VectorLayer } from 'ol/layer'
import { TileWMS ,Vector as VectorSource } from 'ol/source'
export default {
  name: "olMapImageWMSMulLayers",
  data() {
    return {
      map: null, // map地图
      layer:null, //地图图层
      lightLayer:null, //灯图层
      houseLayer:null, //房子图层
      lineLayer:null, //线图层
      lineSource:null, //线数据源
      //红绿灯数据
      lightData:[
        {x:"987798.93778", y:"213885.81024"},
        {x:"987710.93778", y:"213810.81024"},
      ],
      //房子数据
      houseData:[
        {x:"986610.93778", y:"213885.81024"},
        {x:"986510.93778", y:"213810.81024"},
      ],
      //线的数据
      lineData:[
        [986434.4063822062, 215782.0959711917],
        [989701.5290279881,217149.84072807242],
        [990613.3107184113,215946.4192185118],
      ],
   };
  },
  mounted() {
    this.initMap();
    setInterval(() => {
      this.initLightData();
    }, 1000)
  },
  methods: {

    //初始化红绿灯数据
    initLightData(){
      this.lightLayer.getSource().clear();
      this.lightData.forEach((item, index) => {
          var feature = new Feature({
              geometry: new Point([Number(item.x), Number(item.y)]),
          });
          let url = "images/light.png";
          const zoom = this.map.getView().getZoom();
          let style = new Style({
                  image: new Icon({
                      scale: 0.15 * (zoom -13) ,
                      src: url,
                      anchor: [0.48, 0.52],
                  }),
              });
          feature.setStyle(style);
          this.lightLayer.getSource().addFeature(feature);
      });
    },

    //初始化房子数据
    initHouseData(){
      this.houseLayer.getSource().clear();
      this.houseData.forEach((item, index) => {
          var feature = new Feature({
              geometry: new Point([Number(item.x), Number(item.y)]),
          });
          let url = "images/house.png";
          let style = new Style({
                  image: new Icon({
                      scale: 0.3,
                      src: url,
                      anchor: [0.48, 0.52],
                  }),
              });
          feature.setStyle(style);
          this.houseLayer.getSource().addFeature(feature);
      });
    },

    //画线
    drawLine(){
      let pointData = this.lineData; // 所有点位信息
      //下边来添加一线feature
      var feature = new Feature({
          type: "lineStyle",
          geometry: new LineString(
              pointData // 线的坐标
          ),
      });
      let color = 'green';
      let lineStyle = new Style({
          stroke: new Stroke({
              color: color,
              width: 4,
          }),
      });
      // 添加线的样式
      feature.setStyle(lineStyle);
      // 添加线的fature
      this.lineSource.addFeature(feature);
    },
    initMap() {
      //地图图层
      this.layer = new TileLayer({
        source: new TileWMS({
          //不能设置为0,否则地图不展示。
          ratio: 1,
          url: "http://localhost:8000/geoserver/nyc/wms",
          params: {
            LAYERS: "nyc:nyc_roads",
            STYLES: "",
            VERSION: "1.1.1",
            tiled: true
          },
          serverType: "geoserver",
        }),
      });

      // 红绿灯的图层
      this.lightLayer = new VectorLayer({
          source: new VectorSource(),
      });
     
      //房子的图层
      this.houseLayer = new VectorLayer({
          source: new VectorSource(),
      });

      //线的图层
      this.lineSource = new VectorSource({ wrapX: false });
      this.lineLayer = new VectorLayer({
          source: this.lineSource,
      });

      this.map = new Map({
        //地图容器ID
        target: "map",
        //引入地图
        layers: [this.layer,this.lightLayer,this.houseLayer,this.lineLayer],
        view: new View({
          //地图中心点
          center: [987777.93778, 213834.81024],
          zoom: 12,
          minZoom:6, // 地图缩放最小级别
          maxZoom:19,
        }),
      });

      this.initLightData();
      this.initHouseData();
      this.drawLine();
      //获取点的监听方法设置
      this.onPoint()
    },
    //  获取点
    onPoint() {
      // 监听singleclick事件
      this.map.on('singleclick', function(e) {
        console.log(e.coordinate)
      })
    }
  },
};
</script>

<style scoped>
.map {
  width: 100%;
  height: 800px;
}
</style>

​

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
Vue中使用OpenLayers进行面积测量,可以通过以下步骤实现: 1. 首先,在Vue项目中,安装OpenLayers库。可以通过运行以下命令进行安装: ``` npm install ol ``` 2. 在Vue组件中引入OpenLayers库,可以使用`import`语句进行引入: ```javascript import ol from 'ol'; ``` 3. 创建一个Vue组件,并在`mounted`钩子函数中进行OpenLayers地图的初始化。在地图初始化过程中,可以设置视图的初始中心以及缩放级别: ```javascript mounted() { const map = new ol.Map({ target: 'map', view: new ol.View({ center: [0, 0], zoom: 2 }) }); } ``` 4. 在组件的模板中,添加一个具有特定ID的`div`元素,作为地图的容器: ```html <template> <div id="map"></div> </template> ``` 5. 在`mounted`钩子函数中,创建一个用于绘制图形的矢量源和矢量层。可以通过`ol.source.Vector`和`ol.layer.Vector`来实现: ```javascript mounted() { // ...省略地图初始化代码 const vectorSource = new ol.source.Vector(); const vectorLayer = new ol.layer.Vector({ source: vectorSource }); map.addLayer(vectorLayer); } ``` 6. 添加一个测量控件,用于测量面积。可以通过`ol.control.Measure`来实现: ```javascript mounted() { // ...省略地图初始化和添加矢量层代码 const measureControl = new ol.control.Measure({ source: vectorSource, type: 'area', interactionType: 'click' }); map.addControl(measureControl); } ``` 7. 最后,在模板中添加一个按钮或其他元素,用于触发测量功能: ```html <template> <div id="map"> <button @click="startMeasurement">开始测量</button> </div> </template> ``` 8. 在组件的方法中,编写`startMeasurement`方法,用于启动测量功能: ```javascript methods: { startMeasurement() { const measureControl = map.getControls().getArray().find(control => control instanceof ol.control.Measure); measureControl.setActive(true); } } ``` 通过以上步骤,Vue中的OpenLayers地图将具备面积测量功能。用户可以点击“开始测量”按钮,然后在地图绘制多边形,测量结果将以弹窗或其他形式呈现给用户。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

霸道流氓气质

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值