Skip to content

Instantly share code, notes, and snippets.

@Lythom
Last active February 3, 2016 18:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lythom/ae6510e5c59abb37c6ba to your computer and use it in GitHub Desktop.
Save Lythom/ae6510e5c59abb37c6ba to your computer and use it in GitHub Desktop.
static vs. dynamic google map on react component
// 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