Advertisement
ronikuchan

react-google-maps-search

Jun 24th, 2021
1,151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from "react";
  2. import ReactDOM from "react-dom";
  3.  
  4. const _ = require("lodash");
  5. const { compose, withProps, lifecycle } = require("recompose");
  6. const {
  7.     withScriptjs,
  8.     withGoogleMap,
  9.     GoogleMap,
  10.     Marker
  11. } = require("react-google-maps");
  12. const {
  13.     SearchBox
  14. } = require("react-google-maps/lib/components/places/SearchBox");
  15.  
  16. const MapWithASearchBox = compose(
  17.     withProps({
  18.         googleMapURL:
  19.             "https://maps.googleapis.com/maps/api/js?key=API_KEY&v=3.exp&libraries=geometry,drawing,places",
  20.         loadingElement: <div style={{ height: `100%` }} />,
  21.         containerElement: <div style={{ height: `400px` }} />,
  22.         mapElement: <div style={{ height: `100%` }} />
  23.     }),
  24.     lifecycle({
  25.         componentWillMount() {
  26.             const refs = {};
  27.  
  28.             this.setState({
  29.                 bounds: null,
  30.                 center: {
  31.                     lat: -6.2621483,
  32.                     lng: 106.8267923
  33.                 },
  34.                 markers: [],
  35.                 onMapMounted: ref => {
  36.                     refs.map = ref;
  37.                 },
  38.                 onBoundsChanged: () => {
  39.                     this.setState({
  40.                         bounds: refs.map.getBounds(),
  41.                         center: refs.map.getCenter()
  42.                     });
  43.                 },
  44.                 onSearchBoxMounted: ref => {
  45.                     refs.searchBox = ref;
  46.                 },
  47.                 onPlacesChanged: () => {
  48.                     console.log('halo')
  49.                     const places = refs.searchBox.getPlaces();
  50.                     const bounds = new window.google.maps.LatLngBounds();
  51.  
  52.                     places.forEach(place => {
  53.                         if (place.geometry.viewport) {
  54.                             bounds.union(place.geometry.viewport);
  55.                         } else {
  56.                             bounds.extend(place.geometry.location);
  57.                         }
  58.                     });
  59.                     const nextMarkers = places.map(place => ({
  60.                         position: place.geometry.location
  61.                     }));
  62.                     const nextCenter = _.get(
  63.                         nextMarkers,
  64.                         "0.position",
  65.                         this.state.center
  66.                     );
  67.  
  68.                     this.setState({
  69.                         center: nextCenter,
  70.                         markers: nextMarkers
  71.                     });
  72.                     // refs.map.fitBounds(bounds);
  73.                 },
  74.                 onMarkerDragEnd: (coord, index) => {
  75.                     const { latLng } = coord;
  76.                     const lat = latLng.lat();
  77.                     const lng = latLng.lng();
  78.                     console.log(111);
  79.                     this.setState(prevState => {
  80.                         const markers = [...this.state.markers];
  81.                         markers[index] = {
  82.                             ...markers[index],
  83.                             position: { lat, lng }
  84.                         };
  85.                         return { markers };
  86.                     });
  87.                 }
  88.             });
  89.         }
  90.     }),
  91.     withScriptjs,
  92.     withGoogleMap
  93. )(props => (
  94.     <GoogleMap
  95.         ref={props.onMapMounted}
  96.         defaultZoom={13}
  97.         center={props.center}
  98.         onBoundsChanged={props.onBoundsChanged}
  99.         options={{
  100.             streetViewControl: false,
  101.             // styles: "mapsStyle"
  102.             keyboardShortcuts: false,
  103.             scaleControl: false,
  104.             scrollwheel: true,
  105.             disableDefaultUI: true,
  106.             gestureHandling: 'greedy'
  107.         }}
  108.     >
  109.         <SearchBox
  110.             ref={props.onSearchBoxMounted}
  111.             bounds={props.bounds}
  112.             controlPosition={window.google.maps.ControlPosition.TOP_LEFT}
  113.             onPlacesChanged={props.onPlacesChanged}
  114.         >
  115.             <input
  116.                 type="text"
  117.                 placeholder="find your address"
  118.                 style={{
  119.                     boxSizing: `border-box`,
  120.                     border: `1px solid transparent`,
  121.                     width: `97%`,
  122.                     height: `35px`,
  123.                     marginTop: `2px`,
  124.                     marginBottom: `0px`,
  125.                     marginRight: `5px`,
  126.                     marginLeft: `5px`,
  127.                     padding: `0 12px`,
  128.                     borderRadius: `3px`,
  129.                     boxShadow: `0 2px 6px rgba(0, 0, 0, 0.3)`,
  130.                     fontSize: `14px`,
  131.                     outline: `none`,
  132.                     textOverflow: `ellipses`
  133.                 }}
  134.             />
  135.         </SearchBox>
  136.         {props.markers.map((marker, index) => (
  137.             <Marker
  138.                 key={index}
  139.                 position={marker.position}
  140.                 draggable={true}
  141.                 onDragEnd={props.onDragMarker}
  142.             />
  143.         ))}
  144.     </GoogleMap>
  145. ));
  146.  
  147. class MapThree extends Component{
  148.     constructor(props) {
  149.         super(props);
  150.         this.state = {
  151.             markers1: [
  152.                 {
  153.                     name: "Current position",
  154.                     position: {
  155.                         lat: -6.2621483,
  156.                         lng: 106.8267923
  157.                     }
  158.                 }
  159.             ]
  160.         };
  161.     }
  162.  
  163.     onMarkerDragEnd = (coord, index) => {
  164.         const { latLng } = coord;
  165.         const lat = latLng.lat();
  166.         const lng = latLng.lng();
  167.  
  168.         this.setState({
  169.             ...this.state.markers1[index],
  170.             position: { lat, lng }
  171.         });
  172.     };
  173.     render() {
  174.         return (
  175.             <div className="App">
  176.                 <MapWithASearchBox
  177.                     onDragMarker={this.onMarkerDragEnd.bind(this)}
  178.                 />
  179.             </div>
  180.         );
  181.     }
  182. }
  183.  
  184. export default MapThree
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement