0

__ I have used withprops in react-google-maps. i have also used searchbox from react-google-maps my problem is that when onplacechanged event occure i want i am trying to update center in state but this.setState is not accesible in onplacechabged function. i want to access this.setState from RMSelectLocation class but it could'nt accesible. is there any other to imlement google map with autocomplete searchbox in reactjs __

import React, { Component, useEffect } from "react";
import { compose, withProps, lifecycle } from "recompose";
import {
  withScriptjs,
  withGoogleMap,
  GoogleMap,
  Marker,
} from "react-google-maps";
import { get } from "lodash";
import { SearchBox } from "react-google-maps/lib/components/places/SearchBox";
var cntr = { lat: 19.993982, lng: 73.790416 };
class RMSelectLocation extends Component {
  state = {
    locations: "nashik",
    center: { lat: 19.993982, lng: 73.790416 },
    zoom: 12,
    type: "",
  };
  componentDidMount() {}
  render() {
    const MapWithASearchBox = compose(
      withProps({
        googleMapURL:
          "https://maps.googleapis.com/maps/api/js?key=AIzaSyAIVf-f5cukgHjy_ZEv32yc9Liehb4vTGQ&v=3.exp&libraries=geometry,drawing,places",
        loadingElement: <div style={{ height: `100%` }} />,
        containerElement: <div style={{ height: `400px` }} />,
        mapElement: <div style={{ height: `200%` }} />,
      }),
      lifecycle({
        componentWillMount() {
          const refs = {};
          this.setState({
            bounds: null,
            center: {
              lat: 19.9975,
              lng: 73.7898,
            },
            markers: [],
            onMapMounted: (ref) => {
              refs.map = ref;
            },
            onBoundsChanged: () => {
              this.setState({
                bounds: refs.map.getBounds(),
                center: refs.map.getCenter(),
              });
            },
            onSearchBoxMounted: (ref) => {
              refs.searchBox = ref;
            },
            onPlacesChanged: () => {
              console.log(this.state);
              console.log(this.props);
              const places = refs.searchBox.getPlaces();
              const bounds = new window.google.maps.LatLngBounds();
              places.forEach((place) => {
                if (place.geometry.viewport) {
                  bounds.union(place.geometry.viewport);
                } else {
                  bounds.extend(place.geometry.location);
                }
              });
              const nextMarkers = places.map((place) => ({
                position: place.geometry.location,
              }));
              const nextCenter = get(
                nextMarkers,
                "0.position",
                this.state.center
              );
              this.setState({
                center: nextCenter,
                markers: nextMarkers,
              });
            },
          });
        },
      }),
      withScriptjs,
      withGoogleMap
    )((props) => (
      <GoogleMap
        ref={props.onMapMounted}
        defaultZoom={15}
        center={props.center}
        onBoundsChanged={props.onBoundsChanged}
        defaultMapTypeId={"hybrid"}
      >
        <SearchBox
          ref={props.onSearchBoxMounted}
          bounds={props.bounds}
          controlPosition={window.google.maps.ControlPosition.TOP_RIGHT}
          onPlacesChanged={props.onPlacesChanged}
        >
          <input
            type="text"
            placeholder="Customized your placeholder"
            style={{
              boxSizing: `border-box`,
              border: `1px solid transparent`,
              width: `240px`,
              height: `32px`,
              marginTop: `27px`,
              padding: `0 12px`,
              borderRadius: `3px`,
              boxShadow: `0 2px 6px rgba(0, 0, 0, 0.3)`,
              fontSize: `14px`,
              outline: `none`,
              textOverflow: `ellipses`,
            }}
          />
        </SearchBox>
        {props.markers.map((marker, index) => (
          <Marker key={index} position={marker.position} />
        ))}
      </GoogleMap>
    ));
    return (
      
              <MapWithASearchBox />
          
    );
  }
}
export default RMSelectLocation;
vijay kasar
  • 145
  • 1
  • 12

1 Answers1

0

Ciao, if this.setState is not reachable could be a this context problem as explained here. Take a look and let us know :)

Giovanni Esposito
  • 10,696
  • 1
  • 14
  • 30