四十五、openlayers官网示例Icon modification解析——在地图上添加标记图形并随意移动它的位置

官网demo地址:

Icon modification

 这篇讲了如何随意移动地图上的矢量点。

先在地图上添加一个矢量点,其中anchorXUnitsanchorYUnits: 指定锚点的单位。'fraction' 表示相对于图标的宽度(0到1之间),'pixels' 表示以像素为单位。

const iconFeature = new Feature({
      geometry: new Point([0, 0]),
      name: "Null Island",
      population: 4000,
      rainfall: 500,
    });

    const iconStyle = new Style({
      image: new Icon({
        anchor: [0.5, 46], 
        anchorXUnits: "fraction", 
        anchorYUnits: "pixels",
        src: "https://openlayers.org/en/latest/examples/data/icon.png",
      }),
    });

图形能移动的核心代码是Modify类,Modify类可以捕获矢量图形上的点,移动点的位置来改变矢量图形的位置,而hitDetection的作用则是使鼠标更容易捕获到图形上,当用户与地图进行交互(例如点击或拖动要素)时,Modify交互会检查用户的点击位置是否命中了hitDetection参数指定的图层中的要素。如果命中,则触发相应的修改操作,允许用户拖动顶点或整个几何要素进行修改。

const modify = new Modify({
      hitDetection: vectorLayer,
      source: vectorSource,
    });

如果不设置hitDetection,捕获点会困难一些,鼠标必须移动到点坐标的位置才可以捕获到顶点。

添加之前: 

 添加之后,鼠标放在icon图形上即可捕获顶点,

小细节:

Modify的主要作用是修改图形,之所以可以随意移动图形的位置,是因为点或者圆形只有一个顶点坐标构成。如果Modify用在多边形或直线身上, 它会捕获构成图形的任意一个点,通过鼠标拖动来移动点的坐标从而改变形状。而不是直接移动整个图形的位置。

想要随意移动多边形或其他矢量图形的位置还得通过自定义交互类。

参考示例:

二十四、openlayers官网示例Custom Interactions——自定义交互实现在地图上移动、拖拽feature_openlayers feature 拖动-CSDN博客

完整代码:

<template>
  <div class="box">
    <h1>Icon modification</h1>
    <div id="map"></div>
  </div>
</template>

<script>
import Feature from "ol/Feature.js";
import Map from "ol/Map.js";
import Point from "ol/geom/Point.js";
import View from "ol/View.js";
import { Icon, Style } from "ol/style.js";
import { Modify } from "ol/interaction.js";
import { OGCMapTile, Vector as VectorSource } from "ol/source.js";
import { Tile as TileLayer, Vector as VectorLayer } from "ol/layer.js";
export default {
  name: "",
  components: {},
  data() {
    return {
      map: null,
    };
  },
  computed: {},
  created() {},
  mounted() {
    const iconFeature = new Feature({
      geometry: new Point([0, 0]),
      name: "Null Island",
      population: 4000, //自定义属性
      rainfall: 500, //自定义属性
    });

    const iconStyle = new Style({
      image: new Icon({
        anchor: [0.5, 46],
        anchorXUnits: "fraction",
        anchorYUnits: "pixels",
        src: "https://openlayers.org/en/latest/examples/data/icon.png",
      }),
    });

    iconFeature.setStyle(iconStyle);

    const vectorSource = new VectorSource({
      features: [iconFeature],
    });

    const vectorLayer = new VectorLayer({
      source: vectorSource,
    });

    const rasterLayer = new TileLayer({
      source: new OGCMapTile({
        url: "https://maps.gnosis.earth/ogcapi/collections/NaturalEarth:raster:HYP_HR_SR_OB_DR/map/tiles/WebMercatorQuad",
        crossOrigin: "",
      }),
    });

    const target = document.getElementById("map");
    const map = new Map({
      layers: [rasterLayer, vectorLayer],
      target: target,
      view: new View({
        center: [0, 0],
        zoom: 3,
      }),
    });

    const modify = new Modify({
      hitDetection: vectorLayer,
      source: vectorSource,
    });
    modify.on(["modifystart", "modifyend"], function (evt) {
      target.style.cursor = evt.type === "modifystart" ? "grabbing" : "pointer";
    });
    const overlaySource = modify.getOverlay().getSource();
    overlaySource.on(["addfeature", "removefeature"], function (evt) {
      target.style.cursor = evt.type === "addfeature" ? "pointer" : "";
    });

    map.addInteraction(modify);
  },
  methods: {},
};
</script>

<style lang="scss" scoped>
#map {
  width: 100%;
  height: 500px;
}
.box {
  height: 100%;
}
</style>

 

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 在Vue中安装OpenLayers库 ``` npm install ol ``` 2. 在Vue组件中引入OpenLayers库和样式文件 ``` import 'ol/ol.css'; import { Map, View } from 'ol'; import { Tile as TileLayer, Vector as VectorLayer } from 'ol/layer'; import { OSM, Vector as VectorSource } from 'ol/source'; import { Circle as CircleStyle, Fill, Stroke, Style } from 'ol/style'; ``` 3. 创建一个地图容器 ``` <template> <div id="map" style="height: 400px; width: 100%;"></div> </template> ``` 4. 在Vue组件中创建地图对象和图层 ``` <script> export default { name: 'MapComponent', mounted() { // 创建地图容器 const map = new Map({ target: 'map', layers: [ new TileLayer({ source: new OSM() }) ], view: new View({ center: [0, 0], zoom: 2 }) }); // 创建矢量图层 const vectorLayer = new VectorLayer({ source: new VectorSource(), style: new Style({ image: new CircleStyle({ radius: 5, fill: new Fill({ color: 'red' }), stroke: new Stroke({ color: 'white', width: 1 }) }) }) }); // 添加矢量图层到地图 map.addLayer(vectorLayer); // 给定一组归一化坐标数据 const coordinates = [ [0.5, 0.5], [0.6, 0.6], [0.7, 0.4], [0.3, 0.8], [0.9, 0.2] ]; // 将归一化坐标转换为地理坐标 const features = coordinates.map(coord => { return new Feature({ geometry: new Point(fromExtent([coord[0], coord[1], coord[0], coord[1]])) }); }); // 添加要素到矢量图层 vectorLayer.getSource().addFeatures(features); } } </script> ``` 5. 在地图画布上绘制图形 使用`fromExtent`方法将归一化坐标转换为地理坐标,然后创建一个`Point`要素,并添加到矢量图层中。 在样式中使用`CircleStyle`来定义点的样式,并将其作为`Style`对象的`image`属性传递给矢量图层。 最终效果如下图所示: ![map](https://i.loli.net/2021/07/17/1Y8KuV7sGQlJ2wN.png)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值