-
Notifications
You must be signed in to change notification settings - Fork 936
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(GoogleMapLoader): introduce loader component interface #157
Conversation
* Closes #141 * Closes #133 BREAKING CHANGE: GoogleMap with props.containerProps is now deprecated. Use GoogleMapLoader with props.googleMapElement instead We also suggest switching to callback based ref so that you'll get the component instance when it is mounted. Before: ```js <GoogleMap containerProps={{ ...this.props, style: { height: "100%", }, }} ref="map" defaultZoom={3} defaultCenter={{lat: -25.363882, lng: 131.044922}} onClick={::this.handleMapClick}> {this.state.markers.map((marker, index) => { return ( <Marker {...marker} onRightclick={this.handleMarkerRightclick.bind(this, index)} /> ); })} </GoogleMap> ``` After: ```js <GoogleMapLoader containerElement={ <div {...this.props} style={{ height: "100%", }} /> } googleMapElement={ <GoogleMap ref={(map) => console.log(map)} defaultZoom={3} defaultCenter={{lat: -25.363882, lng: 131.044922}} onClick={::this.handleMapClick}> {this.state.markers.map((marker, index) => { return ( <Marker {...marker} onRightclick={this.handleMarkerRightclick.bind(this, index)} /> ); })} </GoogleMap> } /> ```
* Ref #92 BREAKING CHANGE: ScriptjsLoader will delegate to GoogleMapLoader when the script is loaded Before: ```js <ScriptjsLoader hostname={"maps.googleapis.com"} pathname={"/maps/api/js"} query={{v: `3.${ AsyncGettingStarted.version }`, libraries: "geometry,drawing,places"}} loadingElement={ <div {...this.props} style={{ height: "100%" }}> <FaSpinner /> </div> } googleMapElement={ <GoogleMap containerProps={{ ...this.props, style: { height: "100%", }, }} ref={googleMap => { // Wait until GoogleMap is fully loaded. Related to #133 setTimeout(() => { googleMap && console.log(`Zoom: ${ googleMap.getZoom() }`); }, 50); }} defaultZoom={3} defaultCenter={{lat: -25.363882, lng: 131.044922}} onClick={::this.handleMapClick} > <Marker {...this.state.marker} onRightclick={this.handleMarkerRightclick} /> </GoogleMap> } /> ``` After: ```js <ScriptjsLoader hostname={"maps.googleapis.com"} pathname={"/maps/api/js"} query={{v: `3.${ AsyncGettingStarted.version }`, libraries: "geometry,drawing,places"}} loadingElement={ <div {...this.props} style={{ height: "100%" }}> <FaSpinner /> </div> } containerElement={ <div {...this.props} style={{ height: "100%" }} /> } googleMapElement={ <GoogleMap ref={googleMap => { googleMap && console.log(`Zoom: ${ googleMap.getZoom() }`); }} defaultZoom={3} defaultCenter={{lat: -25.363882, lng: 131.044922}} onClick={::this.handleMapClick} > <Marker {...this.state.marker} onRightclick={this.handleMarkerRightclick} /> </GoogleMap> } /> ```
Released v4.6.0 |
Am I doing something wrong or is the ref (with callback) wrong? It's no longer the actual google maps DOM node but the one you're using for the children ( |
Yes, you should get the component instance instead of the google maps DOM node. To access it, you may get the DOM node of containerElement={
<div {...this.props} style={{ height: "100%" }} ref={(dom) => console.log(`THIS is dom: ${ dom }`} />
}
// You may also trigger the event using:
google.maps.event.trigger(dom.firstChild, 'resize'); |
Thanks for this, I had to setTimeout(fn, 0) somewhere and was able to remove it :) |
Looks like this example needs updating: https://tomchentw.github.io/react-google-maps/#places/search-box I'm getting an error when I try to use it with v4.7.0. |
@boosh yeah there are many examples need updating. Wanna help out? |
Ah OK. I'll try :-). I wasn't sure if it was one that slipped through the net. Thanks for the great library. Finally google maps is simple to work with 👍 |
I think the deprecation error messages are confusing. I accidentally copied the "before" ScriptjsLoader example and I got:
So then I tried using
ScriptjsLoader is telling me to use GoogleMapLoader, GoogleMapLoader is telling me to use ScriptjsLoader. It was only by digging deep through the code that I realized I needed to remove the |
Sorry for the confusion @jedwards1211 |
If api is not loaded due to proxy settings (if the url is blocked), i need to show some message. Which is the error callback of the scriptjsloader ? |
@tomchentw May not be suitable for this specific page but are there any examples of testing the google maps components code using these Scriptjsloader and GoogleMapsloader paradigm? |
We're looking into it shortly! We're also looking for maintainers. Involve in #266 to help strengthen our community! |
Breaking Changes
Why for this change?
This PR decouples the creation logic of
google.maps.Map
instance and the mounting logic for<GoogleMap>
component. As a result, we'll have:The correct lifecycle for
<GoogleMap>
componentBecause when it's mounted (
ref
callback invoked with the component instance), it's ready for use: invoking public APIs, reading values with getters ...etcConsistent API with
ScriptjsLoader
See more in the example below.
GoogleMap with props.containerProps is now deprecated.
Use
GoogleMapLoader
withprops.googleMapElement
insteadWe also suggest switching to callback based ref so that you'll get the component instance when it is mounted.
Before:
After:
ScriptjsLoader will delegate to GoogleMapLoader when the script is loaded
Before:
After:
Update examples/gh-pages
You could see the real running code for these new behavior in [examples/gh-pages] folder