OpenLayers 在Vue中画点线面

VUE代码如下

<template>
    <div id="map" ref="rootmap">
        <div style="position: absolute;z-index: 9999; background: ghostwhite;display: flex;">
            <div>Geometry type &nbsp;</div>
            <select v-model="typeSelected" @change="getTypeSelected">
                <option value="Point">Point</option>
                <option value="LineString">LineString</option>
                <option value="Polygon">Polygon</option>
                <option value="Circle">Circle</option>
                <option value="None">None</option>
            </select>
        </div>
    </div>
</template>

<script>
    import "ol/ol.css";
    import { Map, View } from "ol";
    import TileLayer from "ol/layer/Tile";
    import VectorLayer from "ol/layer/Vector"
    import {OSM, TileWMS,Vector} from "ol/source";
    import GeoJSON from "ol/format/GeoJSON";
    import {Style,Stroke} from "ol/style";
    import Draw from "ol/interaction/Draw";

    export default {
        data() {
            return {
                typeSelected:'LineString',
                drawLayer:null,
                draw:null,
                map: null
            };
        },
        mounted() {
            let osmLayer = new TileLayer({
                source: new OSM()
            });
            this.map = new Map({
                target: "map",
                layers: [
                    osmLayer
                ],
                view: new View({
                    projection: "EPSG:4326",    //使用这个坐标系
                    center: [114.064839, 22.548857],  //深圳
                    zoom: 12
                })
            });

            // 添加一个绘制的线使用的layer
            this.drawLayer = new VectorLayer({
                //layer所对应的source
                source: new Vector(),

            })
            //把layer加入到地图中
            this.map.addLayer(this.drawLayer);
        },
        methods:{
            getTypeSelected(){
                this.draw && this.map.removeInteraction(this.draw);
                //再根据typeSelect的值绘制新的Interaction
                this.addInteraction();
            },
            addInteraction() {
                let value = this.typeSelected;
                if (value !== 'None') {
                    this.draw = new Draw({
                        source: this.drawLayer.getSource(),
                        type: this.typeSelected,
                        style: new Style({
                            stroke: new Stroke({
                                color: 'blue',
                                width: 3
                            })
                        })
                    });
                    this.map.addInteraction(this.draw);
                }
            }
        }
    };
</script>

<style>
    #map{height:100%;}
    /*隐藏ol的一些自带元素*/
    .ol-attribution,.ol-zoom { display: none;}
</style>

效果如下

在这里插入图片描述

关键代码

  1. 创建openlayers地图,使用OpenStreetMap的开放地图数据。
     let osmLayer = new TileLayer({
         source: new OSM()
     });
     this.map = new Map({
         target: "map",
         layers: [
             osmLayer,
         ],
         view: new View({
             projection: "EPSG:4326",    //使用这个坐标系
             center: [114.064839, 22.548857],  //深圳
             zoom: 12
         })
     });
  1. 添加一个绘制使用的layer,并添加到地图中
      // 添加一个绘制的线使用的layer
      this.drawLayer = new VectorLayer({
          //layer所对应的source
          source: new Vector(),

      })
      //把layer加入到地图中
      this.map.addLayer(this.drawLayer);
  1. 根据选择类型,进行地图交互
     addInteraction() {
         let value = this.typeSelected;
         if (value !== 'None') {
             this.draw = new Draw({
                 source: this.drawLayer.getSource(),
                 type: this.typeSelected,
                 style: new Style({
                     stroke: new Stroke({
                         color: 'blue',
                         width: 3
                     })
                 })
             });
             this.map.addInteraction(this.draw);
         }
     }
  1. 下拉框值的变化,修改地图交互
     getTypeSelected() {
         this.draw && this.map.removeInteraction(this.draw);
         //再根据typeSelect的值绘制新的Interaction
         this.addInteraction();
     },
OpenLayers是一个开源的JavaScript库,用于在Web上显示地图和地理数据。它提供了丰富的功能和灵活的API,可以用于显示各种地理数据,包括点、线和面。 要在OpenLayers显示GeoJSON的点、线和面,可以按照以下步骤进行操作: 1. 创建一个OpenLayers地图对象: ```javascript var map = new ol.Map({ target: 'map', // 指定地图容器的ID或元素 layers: [ // 添加地图图层 new ol.layer.Tile({ source: new ol.source.OSM() // 使用OpenStreetMap作为底图 }) ], view: new ol.View({ center: [0, 0], // 地图心点的坐标 zoom: 10 // 地图缩放级别 }) }); ``` 2. 创建一个GeoJSON格式的数据源: ```javascript var geojsonObject = { 'type': 'FeatureCollection', 'features': [ // 添加点、线、面的GeoJSON要素 { 'type': 'Feature', 'geometry': { 'type': 'Point', 'coordinates': [0, 0] // 点的坐标 }, 'properties': { 'name': 'Point' // 点的属性信息 } }, { 'type': 'Feature', 'geometry': { 'type': 'LineString', 'coordinates': [[0, 0], [1, 1]] // 线的坐标数组 }, 'properties': { 'name': 'LineString' // 线的属性信息 } }, { 'type': 'Feature', 'geometry': { 'type': 'Polygon', 'coordinates': [[[0, 0], [1, 1], [1, 0], [0, 0]]] // 面的坐标数组 }, 'properties': { 'name': 'Polygon' // 面的属性信息 } } ] }; var vectorSource = new ol.source.Vector({ features: new ol.format.GeoJSON().readFeatures(geojsonObject) }); ``` 3. 创建一个矢量图层,并将数据源添加到图层: ```javascript var vectorLayer = new ol.layer.Vector({ source: vectorSource }); map.addLayer(vectorLayer); // 将矢量图层添加到地图 ``` 通过以上步骤,你就可以在OpenLayers地图上显示GeoJSON的点、线和面了。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值