Saif Ali Khan
Saif Ali Khan

Reputation: 848

How to stop drawing after completing one circle in DrawingManager on react-google-maps?

I have referred to this issue already.

I can implement that also, but the problem is how will I identify if there is some change is the radius because DrawingManager component will be unmounted.

Also I am unable to use function like setDrawingMode(null) because it is not exposed in the react-google-maps public API's, also updating options as drawingControl = false is not working.

Is there any possible other solution through which we can restrict the user to create only one circle and also able to get radius_changed somehow?

Below is the code

<GoogleMap
    defaultZoom={8}
    defaultCenter={new google.maps.LatLng(-34.397, 150.644)}
  >
    <DrawingManager
      defaultDrawingMode={google.maps.drawing.OverlayType.CIRCLE}
      defaultOptions={{
        drawingControl: true,
        drawingControlOptions: {
          position: google.maps.ControlPosition.TOP_CENTER,
          drawingModes: [
            google.maps.drawing.OverlayType.CIRCLE,
          ],
        },
        circleOptions: {
          fillColor: `#ffff00`,
          fillOpacity: 1,
          strokeWeight: 5,
          clickable: false,
          editable: true,
          zIndex: 1,
        },
      }},
      onOverlayComplete={(...args)=> /* what to do to disable drawing??*/}
    />
  </GoogleMap>

Any help will be appreciated. Thanks.

Upvotes: 3

Views: 1704

Answers (1)

Vadim Gremyachev
Vadim Gremyachev

Reputation: 59318

Here is the signature for a onOverlayComplete event:

onOverlayComplete?(e: google.maps.drawing.OverlayCompleteEvent): void 

Since onOverlayComplete event is bound to the instance of DrawingManager class, drawing mode could be disabled like this:

class MyMapComponent extends Component {
  handleOverlayComplete(e){
    this.setDrawingMode(null);
    this.setOptions({drawingControlOptions: {drawingModes: [] }}); 
  };

  render() {
    return (
      <GoogleMap defaultZoom={this.props.zoom} defaultCenter={this.props.center}>
        <DrawingManager
          defaultDrawingMode={google.maps.drawing.OverlayType.CIRCLE}
          defaultOptions={{
            drawingControl: true,
            drawingControlOptions: {
              position: google.maps.ControlPosition.TOP_CENTER,
              drawingModes: [google.maps.drawing.OverlayType.CIRCLE]
            },
            circleOptions: {
              fillColor: `#ffff00`,
              fillOpacity: 1,
              strokeWeight: 5,
              clickable: false,
              editable: true,
              zIndex: 1
            }
          }}
          onOverlayComplete={this.handleOverlayComplete}
        />
      </GoogleMap>
    );
  }
}

Here is a demo

Upvotes: 2

Related Questions