【发布时间】:2016-06-04 10:42:32
【问题描述】:
我正在做一个反应项目,我想要实现的是一个项目列表,旁边有一个谷歌地图。我已经研究了很长时间,寻找最好的方法,我发现了很多例子,但它们并没有真正的表现。 我还希望当您在列表的某个项目上时,地图中相应项目的标记会更改颜色或类似的东西,所以我想将标记与列表中的项目同步。
这是我的地图组件,让您知道我目前正在使用哪些库:
import React from 'react';
import GoogleMaps from 'google-maps-api';
import MapConfig from 'config/maps.json';
import mapStyles from './map.styles';
const MapWithKey = GoogleMaps(MapConfig.API_KEY);
class Map extends React.Component {
render() {
const items = this.props.items;
MapWithKey().then(( maps ) => {
const map = new maps.Map(document.getElementById('js-map'), {
center: {lat: -34.397, lng: 150.644},
scrollwheel: false,
zoom: 8,
mapTypeControl: false,
streetViewControl: false
});
for ( let item of items ) {
new maps.Marker({
position: new maps.LatLng(item.latitude, item.longitude),
map: map
});
}
});
return(
<div style={mapStyles} id="js-map">{this.props.children}</div>
);
}
}
export default Map;
我注意到在开发过程中我无法在状态中添加标记,因为每次添加标记时,组件都会重新渲染。
您知道这样做的好方法并以好的方式构建组件吗? 非常感谢您的帮助。
提前致谢。
【问题讨论】:
标签: google-maps google-maps-api-3 reactjs google-maps-markers