openLayers新手入门三:画点

一、步骤:倒推

map.addLayer(Vector) :将矢量层添加到map

Vector设置source属性:将源添加到矢量层

Source设置feature属性:将矢量对象添加到源

Feature设置geometry属性:将几何添加到矢量对象

Geometry包括简单几何:Point、LineString、Circle

二、思维导图

map:

 source:

三、完整代码

import Map from "ol/Map";
import View from "ol/View";
import TileLayer from "ol/layer/Tile";
import "ol/ol.css";
import * as olProj from "ol/proj";
import TileGrid from "ol/tilegrid/TileGrid";
import TileImage from "ol/source/TileImage";
import XYZ from "ol/source/XYZ";
import Point from "ol/geom/Point";
import Feature from "ol/Feature";
import VectorSource from "ol/source/Vector";
import VectorLayer from "ol/layer/Vector";
import { Circle, Fill, Stroke, Style } from "ol/style";
import OSM from "ol/source/OSM";
import { fromLonLat } from "ol/proj";      

      // 创建几何(圆)
      let point = new Point(olProj.fromLonLat([116.3, 39.9]));

      // 创建样式
      const fill = new Fill({
        color: "green",
      });
      const stroke = new Stroke({
        color: "green",
        width: 25,
      });
      const styles = [
        new Style({
          image: new Circle({
            fill: fill,
            stroke: stroke,
            radius: 5,
          }),
          fill: fill,
          stroke: stroke,
        }),
      ];

      // 创建矢量对象
      let feature = new Feature({
        geometry: point,
      });
      // 创建矢量源
      let source = new VectorSource({});

      // 把要素集合添加到源 addFeatures
      source.addFeature(feature);
      // 创建矢量层
      let vector = new VectorLayer({
        source: source,
        style: styles,
        id: "point1",
      });
      // 把源添加到地图
      this.myMap.addLayer(vector);

四、效果

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值