简体   繁体   English

从多边形获取 GeoJSON

[英]Getting a GeoJSON from a polygon

I am building a ReactJS application that allows users to define plots in a map using an array of coordinates with latitudes and longitudes.我正在构建一个 ReactJS 应用程序,它允许用户使用带有纬度和经度的坐标数组在地图中定义绘图。 I would like to return the GeoJSON of the polygon that the user clicks.我想返回用户单击的多边形的 GeoJSON。

I couldn't find in the documentation how to return the coordinate data when we have multiple arrays.当我们有多个数组时,我在文档中找不到如何返回坐标数据。

As I am doing, it always returns the array with all the coordinates.正如我所做的那样,它总是返回带有所有坐标的数组。 And not just the coordinates of the clicked polygon.而不仅仅是点击多边形的坐标。

My code I put in CodeSandBox我放在CodeSandBox 中的代码

Here's the simulation data come from API这里的模拟数据来自API

 { "coordinates": [ [ [ [25.774, -80.19], [18.466, -66.118], [32.321, -64.757], [25.774, -80.19] ] ], [ [ [25.774, -50.32], [18.466, -36.118], [32.321, -34.757], [25.774, -50.32] ] ] ] }

My function that get click events:我获取点击事件的函数:

 handleClick = (props, polygon, e) => { let geoJSON = { type: "Polygon", coordinates: [] }; const paths = polygon.getPaths().getArray(); for (let path of paths) { let pathArray = []; let points = path.getArray(); let firstPoint = false; for (let point of points) { if (firstPoint === false) { firstPoint = point; } pathArray.push([point.lng(), point.lat()]); } pathArray.push([firstPoint.lng(), firstPoint.lat()]); geoJSON.coordinates.push(pathArray); } console.log("geoJSON", geoJSON); return geoJSON; };

You need to set the value of your coords state to auxCoords.auxCoords since checking the return of your auxCoords, there is another auxCoords that hold the array value of your coordinates.由于检查auxCoords.auxCoords的返回,您需要将 coords 状态的值设置为 auxCoords.auxCoords,还有另一个 auxCoords 保存坐标的数组值。

this.setState({ coords: auxCoords.auxCoords }); Remove the const { coords } = this.state;删除const { coords } = this.state; in your render and the {coords && ()} function that enclose your map object.在您的渲染和包含地图对象的{coords && ()}函数中。

Then, since the array that hold the polygon coordinates are now in this.state.coords, you need to map each array data to the polygon object.然后,由于保存多边形坐标的数组现在在 this.state.coords 中,您需要将每个数组数据映射到多边形对象。 This will create an individual Polygon object for each polygon array.这将为每个多边形数组创建一个单独的 Polygon 对象。 This will then return the specific polygon in the handleclick function.这将在 handleclick 函数中返回特定的多边形。

Here is a sample code and code snippet below:下面是一个示例代码和代码片段:

import React, { Component } from "react";
import { Map, Polygon, GoogleApiWrapper } from "google-maps-react";
import data from "./api/coordinates.json";

export class MapContainer extends Component {
  state = {
    coords: []
  };

  componentDidMount() {
    this.reduceMap();
  }

  reduceMap = () => {
    let auxCoords = {
      auxCoords: data.coordinates.reduce(
        (a, [c]) => a.concat([c.map(([lat, lng]) => ({ lat, lng }))]),
        []
      )
    };
    this.setState({
      coords: auxCoords.auxCoords
    });
  };

  handleClick = (props, polygon, e) => {
    let geoJSON = {
      type: "Polygon",
      coordinates: []
    };

    const paths = polygon.getPaths().getArray();

    for (let path of paths) {
      let pathArray = [];
      let points = path.getArray();
      let firstPoint = false;

      for (let point of points) {
        if (firstPoint === false) {
          firstPoint = point;
        }
        pathArray.push([point.lng(), point.lat()]);
      }

      pathArray.push([firstPoint.lng(), firstPoint.lat()]);
      geoJSON.coordinates.push(pathArray);
    }

    console.log("geoJSON", geoJSON);

    return geoJSON;
  };

  render() {
    return (
      <div>
        <Map
          google={this.props.google}
          style={{ width: "80%", height: "80%", position: "relative" }}
          zoom={3}
          initialCenter={{ lat: 32.321, lng: -64.757 }}
          clickableIcons={false}
          className={"map"}
          center={{ lat: 32.321, lng: -64.757 }}
        >
          {this.state.coords.map((poly, key) => (
            <Polygon
              key={key}
              paths={poly}
              strokeColor="#0000FF"
              strokeOpacity={0.8}
              strokeWeight={2}
              fillColor="#0000FF"
              fillOpacity={0.35}
              onClick={this.handleClick}
            />
          ))}
        </Map>
      </div>
    );
  }
}

export default GoogleApiWrapper({
  apiKey: "YOUR_KEY"
})(MapContainer);

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

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