简体   繁体   English

在拖动反应时获取当前坐标 leaflet

[英]Get current coordinates on dragging react leaflet

I am a newbie of react leaflet.我是反应 leaflet 的新手。 I created my map and add polygon successfully.我创建了我的 map 并成功添加了多边形。 However, when I drag or zoom on my map, how can I get current coordinates of the entire screen?但是,当我拖动或缩放 map 时,如何获取整个屏幕的当前坐标? I use the map of openstreetmap.我使用openstreetmap的map。 Thank you very much for your support.非常感谢您的支持。 My code:我的代码:

class App extends Component {
  componentDidMount()
  {
    //console.log(polygonData);
    navigator.geolocation.getCurrentPosition(function(position) { //mdn geolocation
      console.log(position)
    });
  }

  onEachContry = (feature, layer) =>{
    const contryName = feature.properties.NAME_1;
    //console.log(feature.properties.NAME_1);
    layer.bindPopup(contryName);

    if(contryName == "An Giang")
    {
      layer.options.fillColor = "yellow";
    }

    layer.on({
      /*mouseover: (event) => {
        console.log(event);
      }*/
    }
    )
  }

  countryStyle = {
    fillColor: "red",
    fillOpacity: 0.5,
    color: "black",
    weight: 2
  
  }

  render() {
    return (
        <MapContainer center={[10.7743, 106.6669]} zoom={6} >
          <TileLayer
            attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
            url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
          />
          <GeoJSON
          style = {this.countryStyle}
          data={polygonData.features}
          onEachFeature={this.onEachContry}
          
          />
        </MapContainer>
    );
    }
}

/*<Marker position={[51.505, -0.09]}>
          <Popup>
            A pretty CSS3 popup. <br /> Easily customizable.
          </Popup>
        </Marker>
        */

export default App;

Use a function component as a MapContainer's child.使用 function 组件作为MapContainer's子组件。 There using useMapEvents hook listen to dragend event.那里使用useMapEvents挂钩监听dragend事件。 e.target.getBounds provides everything you need, methods like getSouth() , getWest() , getEast() etc e.target.getBounds提供您需要的一切,例如getSouth()getWest()getEast()等方法

 function MyComponent() {
        const map = useMapEvents({
          dragend: (e) => {
            console.log("mapCenter", e.target.getCenter());
            console.log("map bounds", e.target.getBounds());
          }
        });
        return null;
      }

and then接着

<MapContainer
     ...><MyComponent/>
</MapContainer>

Demo 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM