Advertisement
Guest User

Untitled

a guest
May 24th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from 'react'
  2. import PlacesAutocomplete, { geocodeByAddress, getLatLng } from 'react-places-autocomplete'
  3.  
  4. class LocationSearchInput extends React.Component {
  5.   constructor(props) {
  6.     super(props);  
  7.     this.state = { address: '' }
  8.   }
  9.  
  10.   handleChange = (address) => {
  11.     this.setState({ address })
  12.   }
  13.  
  14.   handleSelect = (address) => {
  15.     geocodeByAddress(address)
  16.       .then(results => getLatLng(results[0]))
  17.       .then(latLng => console.log('Success', latLng))
  18.       .catch(error => console.error('Error', error))
  19.   }
  20.  
  21.   render() {
  22.     return (
  23.       <PlacesAutocomplete
  24.         value={this.state.address}
  25.         onChange={this.handleChange}
  26.         onSelect={this.handleSelect}
  27.       >
  28.         {({ getInputProps, suggestions, getSuggestionItemProps }) => (
  29.           <div>
  30.             <input
  31.               {...getInputProps({
  32.                 placeholder: 'Search Places ...',
  33.                 className: 'location-search-input'
  34.               })}
  35.             />
  36.             <div className="autocomplete-dropdown-container">
  37.               {suggestions.map(suggestion => {
  38.                 const className = suggestion.active ? 'suggestion-item--active' : 'suggestion-item';
  39.                 // inline style for demonstration purpose
  40.                 const style = suggestion.active
  41.                             ? { backgroundColor: '#fafafa', cursor: 'pointer' }
  42.                             : { backgroundColor: '#ffffff', cursor: 'pointer' };
  43.                 return (
  44.                   <div {...getSuggestionItemProps(suggestion, { className, style })}>
  45.                     <span>{suggestion.description}</span>
  46.                   </div>
  47.                 )
  48.               })}
  49.             </div>
  50.           </div>
  51.         )}
  52.       </PlacesAutocomplete>
  53.     );
  54.   }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement