【问题标题】:How to edit existing polygon using react-google-maps using react js如何使用 react-google-maps 使用 react js 编辑现有多边形
【发布时间】:2020-08-02 20:14:29
【问题描述】:

在那之后,我使用绘图管理器添加多边形,然后我想编辑该多边形。当我使用下面的代码时,获取函数未找到错误并且 getPath 函数在我使用获取错误时不起作用。我想添加一个带有位置的城市,但在该代码中它不起作用。当我使用 getPath 函数和可编辑函数时,它会出错。

import { compose, withProps } from "recompose";
import {
  withGoogleMap,
  GoogleMap,
  withScriptjs,
  Polygon,
} from "react-google-maps";
const {
  DrawingManager,
} = require("react-google-maps/lib/components/drawing/DrawingManager");

var polygon = [];
const onAdd = (data) => {
  const path = data.getPath();
  var coordinates = [];
  for (var i = 0; i < path.length; i++) {
    var pathArray = [path.getAt(i).lat(), path.getAt(i).lng()];
    coordinates.push(pathArray);
  }
  polygon = JSON.stringify(coordinates, null, 6);
};

const onEdit = (data) => {
  var pathArray = [data.latLng.lat(), data.latLng.lng()];
  console.log("edit path", pathArray);
  console.log("poly", polygonPath);
  polygonPath.push(pathArray);
};
const onDragEnd = (data) => {
  const path = data.getPath();
  var coordinates = [];
  for (var i = 0; i < path.length; i++) {
    var pathArray = [path.getAt(i).lat(), path.getAt(i).lng()];
    coordinates.push(pathArray);
  }
  console.log("polygon", JSON.stringify(coordinates, null, 6));
};
let polygonPath = [];
const MyMapComponent = compose(
  withProps({
    googleMapURL:
      "https://maps.googleapis.com/maps/api/js?key=" +
      global.config.googleApiKey +
      "&v=3.exp&libraries=geometry,drawing,places",
    loadingElement: <div style={{ height: `100%` }} />,
    containerElement: <div style={{ height: `500px` }} />,
    mapElement: <div style={{ height: `100%` }} />,
  }),
  withScriptjs,
  withGoogleMap
)((props) => {
  let addPolygonPath = [];
  if (props.isEdit) {
    polygonPath = props.isEdit.geometry.coordinates[0];

    addPolygonPath = props.isEdit.geometry.coordinates[0].map(
      (coordinateItem) => {
        return { lat: coordinateItem[0], lng: coordinateItem[1] };
      }
    );
  }

  return (
    <GoogleMap defaultZoom={12} center={props.mapCenter}>
      {props.isEdit ? (
        <Polygon
          editable={true}
          draggable={true}
          paths={addPolygonPath}
          options={{
            strokeWeight: 2,
            fillColor: "#000",
            fillOpacity: 0.4,
            zIndex: 1,
          }}
          onMouseUp={onEdit}
          onDragEnd={onDragEnd}
        />
      ) : (
        <DrawingManager
          defaultOptions={{
            drawingControl: true,
            drawingControlOptions: {
              drawingModes: ["polygon"],
            },

            polygonOptions: {
              strokeWeight: 2,
              fillColor: "#000",
              fillOpacity: 0.4,
              clickable: true,
              editable: true,
              zIndex: 1,
            },
          }}
          onPolygonComplete={onAdd}
        />
      )}
    </GoogleMap>
  );
});

【问题讨论】:

    标签: javascript reactjs polygon react-google-maps


    【解决方案1】:

    您可以使用Polygon 组件上的onLoad 属性来获取对多边形对象的引用。然后将该参考保存在您所在的州。

    您现在可以在每次编辑后使用它来更新您的多边形。

    像这样:

    export default function Map () {
      const [polygon, setPolygon] = React.useState(null);
      const [polygonRef, setPolygonRef] = React.useState(null);
    
      // Util to create a polygon with options
      function createPolygon (path) {
        const polygon = new window.google.maps.Polygon({
          path: path,
          fillColor: "#D43AAC",
          strokeColor: "#EB807E",
          fillOpacity: 0.2,
          strokeWeight: 5,
          editable: true,
          clickable: true,
        });
    
        return polygon;
      };
    
      // When first drawing is complete and edit has been made, this function is run
      const onPolygonComplete = (polygon, destroy=false) => {
        setPolygon(createPolygon(polygon.getPath()));
    
        // Destroys the polygon that has been drawn by the manager.
        if(destroy) {
          polygon.setMap(null);
        }
      };
    
      // Set the ref
      const onPolygonLoad = (polygon) => setPolygonRef(polygon);
    
      // Update the polygon
      const onPolygonMouseUp = () => onPolygonComplete(polygonRef);
    
      const polygonProps = polygon
        ? {
            path: polygon.latLngs.i[0].i,
            editable: polygon.editable,
            clickable: polygon.clickable,
            options: {
              fillOpacity: polygon.fillOpacity,
              strokeWeight: polygon.strokeWeight,
            },
          }
        : null;
    
      return (
        <GoogleMap>
          {/* Map components */}
          {polygon ? (
            <Polygon
              {...polygonProps}
              onLoad={onPolygonLoad}
              onMouseUp={onPolygonMouseUp}
            />
          ) : (
            <DrawingManager
              drawingMode={"polygon"}
              onPolygonComplete={(polygon) => onPolygonComplete(polygon, true)}
            />
          )}
        </GoogleMap>
      );
    };
    

    希望这会有所帮助。 :)

    【讨论】:

    • 我发现您的回答很有用,因为我遇到了类似的问题。虽然我从您发布的解决方案中得到“TypeError:polygon.latLngs.i is undefined”。关于为什么我返回的对象与您得到的不同的任何想法?我在嵌套对象 latLngs 中没有看到 i 而是 fb。谢谢!
    猜你喜欢
    • 2020-01-14
    • 2019-01-14
    • 1970-01-01
    • 2020-12-14
    • 2018-03-09
    • 2020-05-17
    • 1970-01-01
    • 1970-01-01
    • 2020-01-23
    相关资源
    最近更新 更多