Last active
February 3, 2016 18:02
-
-
Save Lythom/ae6510e5c59abb37c6ba to your computer and use it in GitHub Desktop.
static vs. dynamic google map on react component
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// render method of the react component | |
render() { | |
if (this.state.clientSideJsAvailable) { | |
// Executed client side, this.state.clientSideJsAvailable is set in componentDidMount. | |
// return a div, gmap behaviour is plugged and refreshed in componentDidUpdate | |
return <div id="map"></div>; | |
} else { | |
// Executed server side | |
// Executed once client side on first render before mount (to check consistency with intial DOM from the server). | |
// limit to 50 elements to prevent URL from going longer than the limit | |
const markersURLParam = 'scale:2|icon:' + this.props.pinURL + '|' + this.props.stores.slice(0, 50).map(store => store.latitude + ',' + store.longitude).join('|'); | |
// uses scale:2 and halved dimensions to allow a generated map larger than 640px | |
return ( | |
<div> | |
<img | |
src={`http://maps.googleapis.com/maps/api/staticmap?` + | |
`&size=${this.props.width/2}x${this.props.height/2}` + | |
`&scale=2` + | |
`&language=fr` + | |
`&markers=${markersURLParam}` + | |
`&key=${this.props.apiKey}` | |
} | |
style={{width:'100%',height:'auto'}} alt=""/> | |
</div> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment