From 6273309665a2f12feef2b3ec3620c87c5d3e4f07 Mon Sep 17 00:00:00 2001 From: Ivan Date: Mon, 7 Nov 2016 11:07:14 +0200 Subject: [PATCH] #add handlers for fly update layers (#272) * #add handlers for fly update layers * #remove force param * Update google_map.js --- develop/GMapLayers.js | 2 +- src/google_map.js | 24 +++++++++++++++++++----- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/develop/GMapLayers.js b/develop/GMapLayers.js index 4435931..d93f022 100644 --- a/develop/GMapLayers.js +++ b/develop/GMapLayers.js @@ -26,7 +26,7 @@ export const gMap = ({ hoverDistance={hoverDistance} center={center} zoom={zoom} - layerTypes={['TrafficLayer', 'TransitLayer']} + layerTypes={zoom > 12 ? [] : zoom > 10 ? ['TrafficLayer'] : ['TrafficLayer', 'TransitLayer']} onChange={onChange} onChildMouseEnter={onChildMouseEnter} onChildMouseLeave={onChildMouseLeave} diff --git a/src/google_map.js b/src/google_map.js index 0f22ace..eafdf40 100644 --- a/src/google_map.js +++ b/src/google_map.js @@ -127,6 +127,8 @@ export default class GoogleMap extends Component { this.maps_ = null; this.prevBounds_ = null; + this.layers_ = {}; + this.mouse_ = null; this.mouseMoveTime_ = 0; this.boundingRect_ = null; @@ -291,6 +293,14 @@ export default class GoogleMap extends Component { this.map_.setOptions(options); } + + if (nextProps.layerTypes !== this.props.layerTypes) { + for (const layerKey of Object.keys(this.layers_)) { + this.layers_[layerKey].setMap(null); + delete this.layers_[layerKey]; + } + this._setLayers(nextProps.layerTypes); + } } } @@ -384,6 +394,13 @@ export default class GoogleMap extends Component { } } + _setLayers = (layerTypes) => { + layerTypes.forEach((layerType) => { + this.layers_[layerType] = new this.maps_[layerType](); + this.layers_[layerType].setMap(this.map_); + }); + }; + _initMap = () => { // only initialize the map once if (this.initialized_) { @@ -457,14 +474,11 @@ export default class GoogleMap extends Component { const map = new maps.Map(ReactDOM.findDOMNode(this.refs.google_map_dom), mapOptions); - this.props.layerTypes.forEach((layerType) => { - const layer = new maps[layerType](); - layer.setMap(map); - }); - this.map_ = map; this.maps_ = maps; + this._setLayers(this.props.layerTypes); + // render in overlay const this_ = this; const overlay = this.overlay_ = Object.assign(new maps.OverlayView(), {