【问题标题】:Using a Leaflet js plugin in react在反应中使用 Leaflet js 插件
【发布时间】:2019-01-26 15:50:11
【问题描述】:

我想使用一个名为 leaflet-geotiff (https://github.com/stuartmatthews/leaflet-geotiff) 的传单插件,但我正在使用传单反应。我可以将此插件转换为传单反应版本吗?谢谢各位。

【问题讨论】:

    标签: reactjs leaflet react-leaflet geotiff


    【解决方案1】:

    这里是关于如何在react-leaflet 中使用leaflet-geotiff 库的说明:

    1) 安装leaflet-geotiff package

    2) 建议引入以下自定义组件:

    import React, { Component } from "react";
    import { withLeaflet, MapLayer } from "react-leaflet";
    import L from "leaflet";
    
    
    import "leaflet-geotiff"
    import "leaflet-geotiff/leaflet-geotiff-plotty"
    import "leaflet-geotiff/leaflet-geotiff-vector-arrows"
    
    class GeotiffLayer extends MapLayer {
      createLeafletElement(props) {
        const { url, options } = props;
        return L.leafletGeotiff(url, options);
      }
    
      componentDidMount() {
        const { map } = this.props.leaflet;
        this.leafletElement.addTo(map);
      }
    }
    
    export const PlottyGeotiffLayer = withLeaflet(props => {
      const { options, layerRef } = props;
      options.renderer = new L.LeafletGeotiff.Plotty(options);
      return <GeotiffLayer ref={layerRef} {...props} />;
    });
    
    export const VectorArrowsGeotiffLayer = withLeaflet(props => {
      const { options, layerRef } = props;
      options.renderer = new L.LeafletGeotiff.VectorArrows(options);
      return <GeotiffLayer ref={layerRef} {...props} />;
    }); 
    

    3) 最后向地图添加图层

    <Map
        center={this.props.center}
        zoom={this.props.zoom}
        length={4}
    >
        <TileLayer
              url="https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw"
              attribution='<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://mapbox.com">Mapbox</a>'
              id="mapbox.streets"
        />
    
        <PlottyGeotiffLayer
           layerRef={this.windSpeedRef}
           url={windSpeedUrl}
           options={windSpeedOptions}
        />
    
        <VectorArrowsGeotiffLayer
          layerRef={this.windDirectionRef}
          url={windDirectionUrl}
          options={windDirectionOptions}
        />
     </Map>
    

    Here is a demo

    结果

    【讨论】:

    • 谢谢你这很好,唯一的问题是我实际上看不到geotiff的层。它在演示中也不可见
    • @Ryan,令人惊讶的是我可以看到两个图层(矢量箭头和绘图),请参阅更新的答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-03
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 2020-09-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多