openlayers入门(26)绘制点线面

<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: "Polygon", // 当前选中的工具
            tools: [
                // 工具集
                {
                    value: "Point",
                    label: "点",
                },
                {
                    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,
                });
                this.map.addInteraction(this.draw);
            }
        },
    },
    mounted() {
        this.initMap();
    },
};
</script>

<style></style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OpenLayers是一个开源的JavaScript库,用于在Web上渲染交互式地图。它提供了丰富的功能和API,允许用户在地图上添加各种不同的元素,包括点、线和面。 以下是OpenLayers绘制点、线和面的基本步骤: 1. 创建地图对象 使用OpenLayers创建一个地图对象,设置地图中心点和缩放级别。 2. 创建矢量图层 使用OpenLayers创建一个矢量图层,并将其添加到地图中。 3. 创建要素 使用OpenLayers创建一个要素对象,可以是点、线或面。 4. 绘制要素 使用OpenLayers提供的绘制工具,将要素添加到矢量图层中。可以通过鼠标交互或代码方式来进行绘制。 5. 渲染地图 将地图渲染到页面上,可以使用OpenLayers提供的默认样式,也可以自定义样式。 下面是一个简单的示例代码,演示如何使用OpenLayers绘制点、线和面: ``` // 创建地图对象 var map = new ol.Map({ target: 'map', layers: [ new ol.layer.Tile({ source: new ol.source.OSM() }) ], view: new ol.View({ center: ol.proj.fromLonLat([116.38, 39.9]), zoom: 10 }) }); // 创建矢量图层 var vectorLayer = new ol.layer.Vector({ source: new ol.source.Vector(), style: new ol.style.Style({ fill: new ol.style.Fill({ color: 'rgba(255, 255, 255, 0.2)' }), stroke: new ol.style.Stroke({ color: '#ffcc33', width: 2 }), image: new ol.style.Circle({ radius: 7, fill: new ol.style.Fill({ color: '#ffcc33' }) }) }) }); map.addLayer(vectorLayer); // 创建要素 var point = new ol.geom.Point(ol.proj.fromLonLat([116.38, 39.9])); var line = new ol.geom.LineString([ol.proj.fromLonLat([116.38, 39.9]), ol.proj.fromLonLat([116.4, 39.9])]); var polygon = new ol.geom.Polygon([[ ol.proj.fromLonLat([116.38, 39.9]), ol.proj.fromLonLat([116.4, 39.9]), ol.proj.fromLonLat([116.4, 39.92]), ol.proj.fromLonLat([116.38, 39.92]), ol.proj.fromLonLat([116.38, 39.9]) ]]); // 绘制要素 var pointFeature = new ol.Feature(point); var lineFeature = new ol.Feature(line); var polygonFeature = new ol.Feature(polygon); vectorLayer.getSource().addFeatures([pointFeature, lineFeature, polygonFeature]); // 渲染地图 map.render(); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值