UNPKG

@react-google-maps/api

Version:
81 lines (70 loc) 1.96 kB
# Autocomplete example Look at the console.log to see search results ```jsx const { Component } = require('react') const { GoogleMap, LoadScript } = require("../../"); const ScriptLoaded = require("../../docs/ScriptLoaded").default; class MyMapWithAutocomplete extends Component { constructor (props) { super(props) this.autocomplete = null this.onLoad = this.onLoad.bind(this) this.onPlaceChanged = this.onPlaceChanged(this) } onLoad (autocomplete) { console.log('autocomplete: ', autocomplete) this.autocomplete = autocomplete } onPlaceChanged () { if (this.autocomplete !== null) { console.log(this.autocomplete.getPlace()) } else { console.log('Autocomplete is not loaded yet!') } } render () { return ( <ScriptLoaded> <GoogleMap id="searchbox-example" mapContainerStyle={{ height: "400px", width: "800px" }} zoom={2.5} center={{ lat: 38.685, lng: -115.234 }} > <Autocomplete onLoad={this.onLoad} onPlacesChanged={this.onPlaceChanged} > <input type="text" placeholder="Customized your placeholder" style={{ boxSizing: `border-box`, border: `1px solid transparent`, width: `240px`, height: `32px`, padding: `0 12px`, borderRadius: `3px`, boxShadow: `0 2px 6px rgba(0, 0, 0, 0.3)`, fontSize: `14px`, outline: `none`, textOverflow: `ellipses`, position: "absolute", left: "50%", marginLeft: "-120px" }} /> </Autocomplete> </GoogleMap> </ScriptLoaded> ) } } <MyMapWithAutocomplete /> ```