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

这篇具有很好参考价值的文章主要介绍了前端开发---在vue项目中使用openLayers。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

前言

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

效果图

前端开发---在vue项目中使用openLayers,openlayer,vue.js,前端,javascript,前端框架,openlayer

在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
    },

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);
    }

4、重置坐标

CZ () {
      this.vectorSource.clear()
      this.mapX = ''
      this.mapY = ''
    },

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);
},文章来源地址https://www.toymoban.com/news/detail-718671.html

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);
        });
    },

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>

到了这里,关于前端开发---在vue项目中使用openLayers的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • vue+openlayers,初始化openlayers地图,实现鼠标移入、点击、右键等事件

    主要功能:初始化openlayers地图,实现鼠标移入、点击、右键等事件,以及获取当前图标的feature,将当前图标信息以弹框方式进行展示;地图上展示拾取到的经纬度 前端使用的是vue技术栈 步骤一:将地图的公用配置项单独提出成一个js文件,方便打包后进行修改,代码如下

    2024年02月11日
    浏览(27)
  • 一、openlayer开发介绍

    首先需要引入openlayer api开发包。两种方式: 1、import方式,也就是npm安装,npm install ol 2、外部js引入。 下载地址:https://github.com/openlayers/openlayers 历史版本地址:Releases · openlayers/openlayers · GitHub 里边有源码,有打包的文件。 下载后直接放入本地项目,在html文件引入即可 3、

    2024年02月12日
    浏览(21)
  • Element+Vue+OpenLayers webgis实战

    以 ,通过OpenLayers将遥感影像加载到浏览器中,如图1-25所示。 单击“Button”按钮可弹出一个对话框,该对话框的内容为“Hello world”,如图1-26所示。 图1-26所示对话框的实现代码如下: Element组件内部默认使用的是中文,若希望使用其他语言,则需要进行多语言设置,通过

    2024年02月17日
    浏览(32)
  • 【开源WebGIS】07-Openlayers+Vue 测量功能-01

    OpenLayers是一个开源的地图显示引擎,支持距离测量和面积测量。距离测量功能用于测量地图上两点间的直线距离;面积测量功能用于测量地图上一个图形的面积,可以方便的实现在地图上的测量。 基础功能展示 1.1 测量功能按钮和显示结果框的添加 1.2 需要定位一个基础的

    2024年02月06日
    浏览(28)
  • 222:vue+openlayers 实现云雾缭绕,白鸽飞翔的效果

    第222个 点击查看专栏目录 本示例的目的是介绍演示如何在vue+openlayersvue+openlayers: 实现云雾缭绕,白鸽飞翔的效果,这里主要是动态的在canvas上绘制白鸽和云雾效果。 直接复制下面的 vue+openlayers源代码,操作2分钟即可运行实现效果; 注意如果OpenStreetMap无法加载,请加载其他来

    2024年02月04日
    浏览(28)
  • 【GIS开发】OpenLayers在线瓦片数据源汇总

    瓦片地图(切片地图)源于一种大地图解决方案,针对一整块非常大的地图进行切片,分成很多相同大小的小块地图,在用户访问的时候,再一块一块小地图加载,拼接在一起,从而还原成一整块大的地图,如果要提高Web地图的访问速度,使用瓦片地图是非常有效的方法。

    2023年04月24日
    浏览(36)
  • Vue+Openlayers+proj4实现坐标系转换

    Vue中使用Openlayers加载Geoserver发布的TileWMS: Vue中使用Openlayers加载Geoserver发布的TileWMS_霸道流氓气质的博客-CSDN博客 在上面的基础上实现不同坐标系坐标数据的转换。 Openlayers中默认的坐标系是EPSG:900913   EPSG:900913等效于EPSG:3857 可在EPSG官网进行验证   如果从其他坐标系的系统中

    2023年04月27日
    浏览(30)
  • Vue+OpenLayers 创建地图并显示鼠标所在经纬度

    本文用的是高德地图 页面 初始化地图 附css代码

    2024年01月17日
    浏览(37)
  • 243:vue+Openlayers 更改鼠标滚轮缩放地图大小,每次缩放小一点

    第243个 点击查看专栏目录 本示例的目的是介绍如何在vue+openlayers项目中设置鼠标滚轮缩放地图大小,每次滑动一格滚轮,设定的值非默认值1。具体的设置方法,参考源代码。 直接复制下面的 vue+openlayers源代码,操作2分钟即可运行实现效果 示例效果

    2024年02月09日
    浏览(62)
  • vue3结合openlayers,geoserver实现GIS一张图(WebGIS)

            不知不觉一年又要过去了,接触开发也就是这几个月的事情,感觉时间过的真快,今天就是除夕了,祝各位新年快乐呀,话说回来,其实在接触学习WebGIS的过程中还是蛮迷茫的,自己虽然是地信的学生,对于地理方面还有有一些自己的理解,但平时专业课学习的就是

    2024年02月19日
    浏览(38)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包