npm i react-google-maps react-google-autocomplete react-geocode
App.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import React, { Component } from "react"; import ReactDOM from "react-dom"; import Map from "./Map"; import "./styles.css"; class App extends Component { render() { return ( <Map google={this.props.google} center={{ lat: 40.7484, lng: -73.9857 }} height="300px" zoom={15} /> ); } } const rootElement = document.getElementById("root"); ReactDOM.render(<App />, rootElement); export default App |
Map.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 |
import React, { Component } from "react"; import { withGoogleMap, GoogleMap, withScriptjs, InfoWindow, Marker } from "react-google-maps"; import Autocomplete from "react-google-autocomplete"; import Geocode from "react-geocode"; Geocode.setApiKey("AIzaSyBIl2qIEbPhKGA-ODVUmjtGlqF_k8VjWP0"); Geocode.enableDebug(); class Map extends Component { constructor(props) { super(props); this.state = { address: "", city: "", area: "", state: "", mapPosition: { lat: this.props.center.lat, lng: this.props.center.lng }, markerPosition: { lat: this.props.center.lat, lng: this.props.center.lng } }; } /** * Get the current address from the default map position and set those values in the state */ componentDidMount() { Geocode.fromLatLng( this.state.mapPosition.lat, this.state.mapPosition.lng ).then( response => { const address = response.results[0].formatted_address, addressArray = response.results[0].address_components, city = this.getCity(addressArray), area = this.getArea(addressArray), state = this.getState(addressArray); console.log("city", city, area, state); this.setState({ address: address ? address : "", area: area ? area : "", city: city ? city : "", state: state ? state : "" }); }, error => { console.error(error); } ); } /** * Component should only update ( meaning re-render ), when the user selects the address, or drags the pin * * @param nextProps * @param nextState * @return {boolean} */ shouldComponentUpdate(nextProps, nextState) { if ( this.state.markerPosition.lat !== this.props.center.lat || this.state.address !== nextState.address || this.state.city !== nextState.city || this.state.area !== nextState.area || this.state.state !== nextState.state ) { return true; } else if (this.props.center.lat === nextProps.center.lat) { return false; } } /** * Get the city and set the city input value to the one selected * * @param addressArray * @return {string} */ getCity = addressArray => { let city = ""; for (let i = 0; i < addressArray.length; i++) { if ( addressArray[i].types[0] && "administrative_area_level_2" === addressArray[i].types[0] ) { city = addressArray[i].long_name; return city; } } }; /** * Get the area and set the area input value to the one selected * * @param addressArray * @return {string} */ getArea = addressArray => { let area = ""; for (let i = 0; i < addressArray.length; i++) { if (addressArray[i].types[0]) { for (let j = 0; j < addressArray[i].types.length; j++) { if ( "sublocality_level_1" === addressArray[i].types[j] || "locality" === addressArray[i].types[j] ) { area = addressArray[i].long_name; return area; } } } } }; /** * Get the address and set the address input value to the one selected * * @param addressArray * @return {string} */ getState = addressArray => { let state = ""; for (let i = 0; i < addressArray.length; i++) { for (let i = 0; i < addressArray.length; i++) { if ( addressArray[i].types[0] && "administrative_area_level_1" === addressArray[i].types[0] ) { state = addressArray[i].long_name; return state; } } } }; /** * And function for city,state and address input * @param event */ onChange = event => { this.setState({ [event.target.name]: event.target.value }); }; /** * This Event triggers when the marker window is closed * * @param event */ onInfoWindowClose = event => {}; /** * When the user types an address in the search box * @param place */ onPlaceSelected = place => { const address = place.formatted_address, addressArray = place.address_components, city = this.getCity(addressArray), area = this.getArea(addressArray), state = this.getState(addressArray), latValue = place.geometry.location.lat(), lngValue = place.geometry.location.lng(); // Set these values in the state. this.setState({ address: address ? address : "", area: area ? area : "", city: city ? city : "", state: state ? state : "", markerPosition: { lat: latValue, lng: lngValue }, mapPosition: { lat: latValue, lng: lngValue } }); }; /** * When the marker is dragged you get the lat and long using the functions available from event object. * Use geocode to get the address, city, area and state from the lat and lng positions. * And then set those values in the state. * * @param event */ onMarkerDragEnd = event => { console.log("event", event); let newLat = event.latLng.lat(), newLng = event.latLng.lng(), addressArray = []; Geocode.fromLatLng(newLat, newLng).then( response => { const address = response.results[0].formatted_address, addressArray = response.results[0].address_components, city = this.getCity(addressArray), area = this.getArea(addressArray), state = this.getState(addressArray); this.setState({ address: address ? address : "", area: area ? area : "", city: city ? city : "", state: state ? state : "" }); }, error => { console.error(error); } ); }; render() { const AsyncMap = withScriptjs( withGoogleMap(props => ( <GoogleMap google={this.props.google} defaultZoom={this.props.zoom} defaultCenter={{ lat: this.state.mapPosition.lat, lng: this.state.mapPosition.lng }} > {/* For Auto complete Search Box */} <Autocomplete style={{ width: "100%", height: "40px", paddingLeft: "16px", marginTop: "2px", marginBottom: "100px" }} onPlaceSelected={this.onPlaceSelected} types={["(regions)"]} /> {/*Marker*/} <Marker google={this.props.google} name={"Dolores park"} draggable={true} onDragEnd={this.onMarkerDragEnd} position={{ lat: this.state.markerPosition.lat, lng: this.state.markerPosition.lng }} /> <Marker /> {/* InfoWindow on top of marker */} <InfoWindow onClose={this.onInfoWindowClose} position={{ lat: this.state.markerPosition.lat + 0.0018, lng: this.state.markerPosition.lng }} > <div> <span style={{ padding: 0, margin: 0 }}> {this.state.address} </span> </div> </InfoWindow> </GoogleMap> )) ); let map; if (this.props.center.lat !== undefined) { map = ( <div> <div> <div className="form-group"> <label htmlFor="">City</label> <input type="text" name="city" className="form-control" onChange={this.onChange} readOnly="readOnly" value={this.state.city} /> </div> <div className="form-group"> <label htmlFor="">Area</label> <input type="text" name="area" className="form-control" onChange={this.onChange} readOnly="readOnly" value={this.state.area} /> </div> <div className="form-group"> <label htmlFor="">State</label> <input type="text" name="state" className="form-control" onChange={this.onChange} readOnly="readOnly" value={this.state.state} /> </div> <div className="form-group"> <label htmlFor="">Address</label> <input type="text" name="address" className="form-control" onChange={this.onChange} readOnly="readOnly" value={this.state.address} /> </div> </div> <AsyncMap googleMapURL="https://maps.googleapis.com/maps/api/js?key=AIzaSyBIl2qIEbPhKGA-ODVUmjtGlqF_k8VjWP0&libraries=places" loadingElement={<div style={{ height: `100%` }} />} containerElement={<div style={{ height: this.props.height }} />} mapElement={<div style={{ height: `100%` }} />} /> </div> ); } else { map = <div style={{ height: this.props.height }} />; } return map; } } export default Map; |