使用google map s react npm包,我可以获得对映射的dom节点的引用,如下所示:
loadMap() {
const maps = this.props.google.maps;
const mapRef = this.refs.map; <---- ref set in render function on dom node
const node = ReactDOM.findDOMNode(mapRef); <--- the dom node
...
this.map = new maps.Map(node, mapConfig);
this.mapRef = mapRef;
}
render() {
return (
<div style={{ height: mapHeight }} ref="map">
...
然后用来设置
node
然后用来更新地图。
用地图标记怎么做?标记不需要创建dom节点,因此无法获取对标记的引用。
this.marker = new google.maps.Marker({someOptions}); <----- no dom node needed
我想这样做是因为我需要根据redux存储中的某个值动态更改标记的图标。我试过通过道具来改变图标(见下文),但它以某种方式阻止了图标标记的可拖动性,即使draggable设置为true。
return (
<Marker
key={foo}
position={latLngPos}
icon={ active ? activeIcon : defaultIcon }
draggable={true}
onDragstart={() => { return this.handleMapMarkerDragStart();}}
onDragend={() => { return this.handleMapMarkerDrop();}}
/>);
我怀疑事情有点奇怪,因为要让google的maps api与react一起工作,组件必须处理实际的dom节点,而不是虚拟的dom节点。
如果您对此有任何了解,我们将不胜感激。