首頁 > 軟體

Openlayer add mark及新增hover效果範例詳解

2022-11-21 14:01:57

add mark

方法一

如果有多個點的話,可以生成多個 feature(迴圈呼叫 addFeature

const iconStyle = () =>
  new Style({ image: new Icon({ scale: 0.2, src: image }) });
const addFeature = (point: Coordinate) =>
  new Feature({
    geometry: new Point(Proj.fromLonLat(point)),
    properties,
    name: "當前位置",
    population: 4000,
    rainfall: 500,
  });
const pointSource = new VectorSource({
  features: [addFeature(point)],
});
const clusterSourceForLayer = new Cluster({
  source: pointSource,
  distance: 50,
});
const pointLayer = new VectorLayer({
  source: clusterSourceForLayer,
  zIndex: 3,
  style: iconStyle,
});
map.addLayer(pointLayer);
pointLayer.set("baseMap", "iconLayer");

方法二

geojson 去渲染 mark

const iconStyle = () =>
  new Style({ image: new Icon({ scale: 0.2, src: image }) });
const pointSource = new VectorSource({
  features: new GeoJSON().readFeatures(geojson, {
    dataProjection: "EPSG:4326",
    featureProjection: "EPSG:3857",
  }),
});
const clusterSourceForLayer = new Cluster({
  source: pointSource,
  distance: 50,
});
const pointLayer = new VectorLayer({
  source: clusterSourceForLayer,
  zIndex: 3,
  style: iconStyle,
});
map?.addLayer(pointLayer);
pointLayer.set("baseMap", "iconLayer");

geojson 格式

生成 geojson 的方式:

  • 自己手動構建
  • 使用 @turf/helpers,它提供了 pointfeatureCollection 等方法
{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
        "id": "customer002",
        "name": "c2"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [119.777738303153, 32.91324329434815]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "id": "customerId007",
        "name": "張三"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [109.54393448864997, 35.7427088696462]
      }
    }
  ]
}

hover mark

popover

overlay 需要一個 dom 元素,這裡是用過 ref 獲取的

const o = new Overlay({ element: ref.current });
map?.addOverlay(o);
setOverlay(o);

方法一

select 去做,它有個 select 事件

它事件引數中,有個 selected,如果不是空陣列,說明你滑鼠正在 hover mark,就可以彈出 popover,顯示你想要顯示的內容

const select = new Select({
  condition: pointerMove,
  hitTolerance: 1,
  layers: [iconLayer],
  style: iconStyle,
});
select.on("select", (e) => {
  const { selected } = e;
  if (selected.length) {
    const [feature] = selected;
    const _feature = feature.get("features")[0];
    const id = _feature.get("id");
    const name = _feature.get("name");
    setContent({ name, id });
    const coordinate = feature.get("geometry").flatCoordinates;
    overlay.setPosition(coordinate);
  } else {
    overlay.setPosition(undefined);
  }
});
map?.addInteraction(select);

方法二

select 去做,本質也是通過 pointerMove 事件,所以可以直接在 map 上監聽 pointerMove 事件

具體的實現方式我沒有去嘗試,通過上面的推斷,我覺得是可行的

map.on("pointerMove", (e) => {});

clear mark

通過 useEffect 返回的函數清理地圖中的 mark

useEffect(() => {
  return () => {
    // 解除安裝頁面中的 mark
    iconSource?.getFeatures().forEach((feature) => {
      iconSource?.removeFeature(feature);
    });
  };
}, [points, iconSource]);

方法 addInteraction、addOverlay、addLayer 區別

我沒有搞清楚他們之間的區別,目前瞭解的是:

  • addLayer:是新增圖層,圖層分為:
    • 柵格:Tile(圖片)
    • 向量:Vectorgeojsonlerc
    • 向量柵格:VectorTilepbf
  • addInteraction:新增 SelectModify
  • addOverlay:新增 OverlayControl
    • Popover 可以用 Overlay 去做

以上就是Openlayer add mark及新增hover效果範例詳解的詳細內容,更多關於Openlayer新增mark hover效果的資料請關注it145.com其它相關文章!


IT145.com E-mail:sddin#qq.com