• 前端开发---在vue项目中使用openLayers


    前言

    本篇文章主要讲解openLayers的初步使用,包括渲染地图、获取点坐标、标记点、中心位置的调整、以及获取到经纬度向后台发送请求
    演示地址
    官网
    gitee链接

    效果图

    在这里插入图片描述

    在vue中渲染地图

    安装ol插件

    npm install ol

    1、调用插件

    import “ol/ol.css”;
    import { Map, View, ol } from “ol”;
    import TileLayer from “ol/layer/Tile”;

    2、 初始话地图

    /**
         * 初始化地图
         */
        initMap () {
          var that = this
          // 创建地图中心点坐标
          const centerCoordinate = [0, 0];
    
          // 初始化视图对象
          const view = new View({
            center: centerCoordinate,
            zoom: 3,
            projection: "EPSG:4326",
          });
    
          // 创建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}"
              url: "http://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineStreetPurplishBlue/MapServer/tile/{z}/{y}/{x}"
    
            })
          });
          // 初始化地图对象
          this.map = new Map({
            target: this.$refs.mapContainer,
            layers: [
              arcGISLayer
            ],
            view: view
          });
          //鼠标单击事件
          this.map.on('singleclick', function (e) {
            that.mapX = e.coordinate[0]
            that.mapY = e.coordinate[1]
            that.addVectorLabel(e.coordinate)
          });
    
          return this.map
        },
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    3、地图点击事件

    // 定义点
        createLabelStyle (feature) {
          return new Style({
            /**{olx.style.IconOptions}类型*/
            image: new Icon(
              ({
                anchor: [0.5, 60],
                anchorOrigin: 'top-right',
                anchorXUnits: 'fraction',
                anchorYUnits: 'pixels',
                offsetOrigin: 'top-right',
                // offset:[0,10],
                //图标缩放比例
                scale: 0.1,
                //透明度
                opacity: 0.75,
                //图标的url
                src: require("../assets/gd.png")
              })
            )
          });
        },
    
        // 添加坐标点
        addVectorLabel (coordinate) {
          if (this.vectorSource) {
            this.vectorSource.clear()
          } else {
            //矢量标注的数据源
            this.vectorSource = new VectorSource({
              features: []
            })
          }
    
          // //矢量标注图层
          this.vectorLayer = new VectorLayer({
            source: this.vectorSource
          });
          this.map.addLayer(this.vectorLayer);
          //新建一个要素
          var newFeature = new Feature({
            //几何信息
            geometry: new Point(coordinate)
          });
          //设置要素的样式
          newFeature.setStyle(this.createLabelStyle(newFeature));
          this.vectorSource.addFeature(newFeature);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    4、重置坐标

    CZ () {
          this.vectorSource.clear()
          this.mapX = ''
          this.mapY = ''
        },
    
    • 1
    • 2
    • 3
    • 4
    • 5

    5、通过坐标改变视图

    DW () {
    var view = this.map.getView();
    var py = ([parseInt(this.mapX), parseInt(this.mapY)]);
    //平移地图
    view.setCenter(py);
    this.addVectorLabel([this.mapX, this.mapY])
    view.setZoom(9);
    },

    6、保存坐标点

    BC () {
          var parpms = {
            mapX: this.mapX,
            mapY: this.mapY
          }
          const instance = axios.create({
            baseURL: 'https://127.0.0.1'
          });
          instance.post('/api/data', parpms)
            .then(response => {
              // response.data;//请求返回的数据
            })
            .catch(error => {
              console.log(error);
            });
        },
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    vue中使用的源码

    <template>
      <div>
        <div id="map-container" ref="mapContainer" class="map-container"></div>
        <div class="formList">
          <div class="input">
            <div class="name">北纬:</div>
            <el-input v-model="mapX" placeholder="请输入内容"></el-input>
          </div>
          <div class="input">
            <div class="name">东经:</div>
            <el-input v-model="mapY" placeholder="请输入内容"></el-input>
          </div>
          <div class="button" @click='CZ'>重置</div>
          <div class="button" @click='DW'>定位</div>
          <div class="button" @click='BC'>保存</div>
        </div>
      </div>
    
    </template>
    
    <script>
    import "ol/ol.css";
    
    import { fromLonLat } from "ol/proj";
    import { OSM, Vector as VectorSource, Raster as RasterSource } from "ol/source";
    import { Vector as VectorLayer } from "ol/layer";
    import { Fill, Style, Stroke, Icon, Circle as CircleStyle } from "ol/style";
    import { Point } from "ol/geom"; //标点,画线
    import Feature from "ol/Feature";
    import { Map, View, ol } from "ol";
    import TileLayer from "ol/layer/Tile";
    import XYZ from "ol/source/XYZ";
    import axios from 'axios';
    
    export default {
      name: "MapComponent",
      data () {
        return {
          mapX: '',
          mapY: '',
        };
      },
      mounted () {
        this.map = this.initMap()
      },
      methods: {
        /**
         * 初始化地图
         */
        initMap () {
          var that = this
          // 创建地图中心点坐标
          const centerCoordinate = [0, 0];
    
          // 初始化视图对象
          const view = new View({
            center: centerCoordinate,
            zoom: 3,
            projection: "EPSG:4326",
          });
    
          // 创建ArcGIS World Street Map图层
          const arcGISLayer = new TileLayer({
            source: new XYZ({
              url: "http://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineStreetPurplishBlue/MapServer/tile/{z}/{y}/{x}"
    
            })
          });
          // 初始化地图对象
          this.map = new Map({
            target: this.$refs.mapContainer,
            layers: [
              arcGISLayer
            ],
            view: view
          });
          //鼠标单击事件
          this.map.on('singleclick', function (e) {
            that.mapX = e.coordinate[0]
            that.mapY = e.coordinate[1]
            that.addVectorLabel(e.coordinate)
          });
    
          return this.map
        },
    
        CZ () {
          this.vectorSource.clear()
          this.mapX = ''
          this.mapY = ''
        },
    
        DW () {
          var view = this.map.getView();
          var py = ([parseInt(this.mapX), parseInt(this.mapY)]);
          //平移地图
          view.setCenter(py);
          this.addVectorLabel([this.mapX, this.mapY])
          view.setZoom(9);
        },
        BC () {
          var parpms = {
            mapX: this.mapX,
            mapY: this.mapY
          }
          const instance = axios.create({
            baseURL: 'https://127.0.0.1'
          });
          instance.post('/api/data', parpms)
            .then(response => {
              // response.data;//请求返回的数据
            })
            .catch(error => {
              console.log(error);
            });
        },
    
    
    
    
    
        // 定义点
        createLabelStyle (feature) {
          return new Style({
            /**{olx.style.IconOptions}类型*/
            image: new Icon(
              ({
                anchor: [0.5, 60],
                anchorOrigin: 'top-right',
                anchorXUnits: 'fraction',
                anchorYUnits: 'pixels',
                offsetOrigin: 'top-right',
                // offset:[0,10],
                //图标缩放比例
                scale: 0.1,
                //透明度
                opacity: 0.75,
                //图标的url
                src: require("../assets/gd.png")
              })
            )
          });
        },
    
        // 添加坐标点
        addVectorLabel (coordinate) {
          if (this.vectorSource) {
            this.vectorSource.clear()
          } else {
            //矢量标注的数据源
            this.vectorSource = new VectorSource({
              features: []
            })
          }
    
          // //矢量标注图层
          this.vectorLayer = new VectorLayer({
            source: this.vectorSource
          });
          this.map.addLayer(this.vectorLayer);
          //新建一个要素
          var newFeature = new Feature({
            //几何信息
            geometry: new Point(coordinate)
          });
          //设置要素的样式
          newFeature.setStyle(this.createLabelStyle(newFeature));
          this.vectorSource.addFeature(newFeature);
        }
      }
    };
    </script>
    
    <style>
    .map-container {
      width: 100%;
      height: 100vh;
      margin: 0;
      padding: 0;
    }
    
    .formList {
      position: fixed;
      top: 10px;
      left: 50px;
      display: flex;
    }
    
    .formList div {
      margin-left: 20px;
    }
    
    .button {
      width: 50px;
      line-height: 40px;
      background-color: #f68e41;
      border-radius: 3px;
      color: #fff;
    }
    
    .input {
      display: flex;
    }
    
    .input .name {
      line-height: 40px;
      width: 25%;
    }
    </style>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
  • 相关阅读:
    如何预防最新的Mallox变种malloxx勒索病毒感染您的计算机?
    香港服务器运行不正常原因简析
    JavaScript基础知识09——数据类型
    java计算机毕业设计税务缴纳管理系统源程序+mysql+系统+lw文档+远程调试
    推荐一个图像生成开源项目——Fooocus
    华为云云耀云服务器L实例评测|利用服务器打造可视化运维管理中心
    ES6 Promise、Generator与async简单介绍与应用
    C++项目实战——基于多设计模式下的同步&异步日志系统-⑥-日志等级类与日志消息类设计
    python自学入门(打卡十二)2022-12-04
    机器学习基础-数据分析:房价预测
  • 原文地址:https://blog.csdn.net/m0_50207524/article/details/134088626