openlayers 入门(28)自由绘制图形

<template>
    <div class="vm">
        <h2 class="h-title">自由绘制绘制图形</h2>
        <div class="tools-x">
            <select id="type" v-model="tool">
                <option
                    v-for="item in tools"
                    :key="item.value"
                    :value="item.value"
                    >{{ item.label }}</option
                >
            </select>
        </div>
        <div ref="map" class="map-x"></div>
    </div>
</template>

<script>
import "ol/ol.css";
import { Map, View } from "ol";
import Tile from "ol/layer/Tile";
import XYZ from "ol/source/XYZ";
import LayerVector from "ol/layer/Vector";
import SourceVector from "ol/source/Vector";
import Draw from "ol/interaction/Draw";

export default {
    data() {
        return {
            tool: "LineString", // 当前选中的工具
            tools: [
                // 工具集
                {
                    value: "LineString",
                    label: "线条",
                },
                {
                    value: "Polygon",
                    label: "多边形",
                },
                {
                    value: "Circle",
                    label: "圆",
                },
                {
                    value: "None",
                    label: "无",
                },
            ],
            map: null, // 地图
            draw: null,
            source: new SourceVector({
                wrapX: false, // 禁止横向无限重复(底图渲染的时候会横向无限重复),设置了这个属性,可以让绘制的图形不跟随底图横向无限重复
            }),
        };
    },
    watch: {
        tool(newVal) {
            this.addInteraction();
        },
    },
    methods: {
        initMap() {
            let raster = new Tile({
                source: new XYZ({
                    url:
                        "http://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineStreetPurplishBlue/MapServer/tile/{z}/{y}/{x}",
                }),
            });

            let vector = new LayerVector({
                source: this.source,
            });
            this.map = new Map({
                target: this.$refs.map,
                layers: [raster, vector],
                view: new View({
                    projection: "EPSG:4326",
                    center: [113.1206, 23.034996],
                    zoom: 10,
                }),
            });
            this.addInteraction();
        },
        addInteraction() {
            if (this.draw !== null) {
                this.map.removeInteraction(this.draw);
            }
            if (this.tool !== "None") {
                this.draw = new Draw({
                    source: this.source,
                    type: this.tool,
                    freehand: true,
                });
                this.map.addInteraction(this.draw);
            }
        },
    },
    mounted() {
        this.initMap();
    },
};
</script>

<style></style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OpenLayers是一种用于Web地图应用程序的JavaScript库,它提供了丰富的功能和工具来实现地图的可视化展示和交互。而GeoJSON是一种基于JavaScript对象表示法(JSON)的地理空间数据格式,它可以用来描述地理要素和属性。 要使用OpenLayers根据GeoJSON绘制图形,我们可以按照以下步骤进行: 1. 引入OpenLayers库和相关的样式表文件到我们的HTML页面中。 2. 创建一个包含地图的容器元素。例如,可以在HTML中创建一个div元素,并给它一个唯一的id作为标识。 3. 使用JavaScript代码来初始化地图。首先,我们需要创建一个地图对象,并指定它的目标容器为前面创建的容器元素。然后,我们可以设置地图的视图和图层。 4. 创建一个矢量图层,用于显示我们的GeoJSON数据。我们可以使用OpenLayers提供的ol.layer.Vector类来创建一个矢量图层。 5. 通过Ajax获取或直接赋值一个包含GeoJSON数据的JSON对象。 6. 创建一个数据源对象,用于加载GeoJSON数据。我们可以使用OpenLayers提供的ol.source.Vector类来创建一个数据源对象,并传入我们的GeoJSON数据。 7. 创建一个要素对象,将其添加到数据源对象中,然后将数据源对象添加到矢量图层中。 8. 将矢量图层添加到地图中。 通过以上步骤,我们就可以使用OpenLayers根据GeoJSON数据绘制图形了。这些图形可以是点、线或面等地理要素。在绘制完成后,我们还可以根据需要设置图形的样式、交互操作、弹出窗口等其他功能。 值得注意的是,OpenLayers还提供了许多其他功能和方法,可以帮助我们对地图进行更多的操作和自定义
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值