tomchentw / react-google-maps

React.js Google Maps integration component
https://tomchentw.github.io/react-google-maps/
MIT License
4.63k stars 935 forks source link

When I click on button, how to reset the drawing manager? #1044

Open AarjuRabadiya opened 4 years ago

AarjuRabadiya commented 4 years ago

I want to reset the drawing manager. If I use data.setMap(null) then the drawing manager is reset but I want to reset polygon when I click on the reset button.

const onAdd = (data) => {
  const path = data.getPath();

  var pathArray = [];
  for (var i = 0; i < path.length; i++) {
    pathArray = [path.getAt(i).lat(), path.getAt(i).lng()];
    polygonArray.push(pathArray);
  }
  //data.setMap(null);
  console.log("onAdd", polygonArray);
};

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) => {
  if (props.isEdit) {
    polygonArray = props.alreadyExistCity.map((coordinateItem) => {
      // console.log(coordinateItem);

      return { lat: coordinateItem[0], lng: coordinateItem[1] };
    });
  }
  return (
    <GoogleMap defaultZoom={12} center={props.mapCenter}>
      {props.isEdit && polygonArray.length > 0 ? (
        <Polygon
          path={polygonArray}
          options={{
            strokeWeight: 2,
            fillColor: "#000",
            fillOpacity: 0.4,
          }}
        />
      ) : (
        <DrawingManager
          defaultOptions={{
            drawingControl: true,
            drawingControlOptions: {
              drawingModes: ["polygon"],
            },

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

@aarazoo The repo of this project is unmaintained more than a year, and we had build new version https://www.npmjs.com/package/@react-google-maps/api

We had rewrite it to TypeScript, and updating it frequently: https://github.com/JustFly1984/react-google-maps-api/tree/master/packages/react-google-maps-api You can enjoy autocomplete.

You can see our docs: https://react-google-maps-api-docs.netlify.app

Also a lot of examples: https://react-google-maps-api-gatsby-demo.netlify.app/ https://github.com/JustFly1984/react-google-maps-api/tree/master/packages/react-google-maps-api-gatsby-example/src/examples

The bundle size is much smaller: https://bundlephobia.com/result?p=@react-google-maps/api

Our Spectrum community: https://spectrum.chat/react-google-maps
Our Slack channel: https://join.slack.com/t/react-google-maps-api/shared_invite/enQtODc5ODU1NTY5MzQ4LTBiNTYzZmY1YmVjYzJhZThkMGU0YzUwZjJkNGJmYjk4YjQyYjZhMDk2YThlZGEzNDc0M2RhNjBmMWE4ZTJiMjQ

Enjoy!