反应谷歌地图,更新标记OnIdle

时间:2017-07-05 08:22:19

标签: javascript google-maps reactjs

我想获取地图容器角并转换为谷歌地图坐标之后onIdle事件我想要更新标记和我的列表组件, 使用React-google-maps https://github.com/istarkov/google-map-react 请帮忙 , 感谢

import _ from "lodash";

import React , {Component , PropTypes} from 'react';
import {Col} from 'antd';

import { withGoogleMap, GoogleMap, Marker , Polygon } from "react-google-maps";
import MarkerClusterer from "react-google-maps/lib/addons/MarkerClusterer";
import withScriptjs from 'react-google-maps/lib/async/withScriptjs';

const AsyncGoogleMap = _.flowRight(
  withScriptjs,
  withGoogleMap
)(props => (
  <GoogleMap
    ref={props.onMapLoad }
    onIdle={props.onMapIdle}
    defaultZoom={14}
    defaultCenter={{ lat: 35.7206037, lng: 51.38875 }}
  >
    <MarkerClusterer
      averageCenter
      enableRetinaIcons
      gridSize={2}
    >
    {/*imagePath={'./styles/images/blue-pin'}*/}
    {props.markers.map(marker => (
      <Marker
        icon={{
          url: './styles/images/blue-pin.png'
        }}
        position={{ lat: marker.lat, lng: marker.lon }}
        key={marker.id}
      />
    ))}
    </MarkerClusterer>
    {/*<Polygon path={path} /> */}
  </GoogleMap>
));
export default class AsyncGoogleMapComponent extends Component {
  constructor(props){
    super(props)
    this.state = {
      markers: [],
    };

  }
  componentWillReceiveProps(newProps){
    {/*console.log(newProps.projects); */}
    this.setState({
      markers: newProps.projects
    })
  }
  componentDidMount(){

  }
  render() {
    {/*console.log(this.state.markers); */}
    return (
      <Col span={12} className="gutter-row card_group" ref="child">
        <AsyncGoogleMap
          googleMapURL="https://maps.googleapis.com/maps/api/js?key=AIzaSyBebpbsHU358P8sfvxfn7fFmsQIB2UNk94"
          loadingElement={
            <div style={{ height: `100%` }}>

            </div>
          }
          containerElement={
            <div style={{ height: `100%` }} />
          }
          mapElement={
            <div id='googlemap' ref="targetDiv" style={{ height: `100%` }} />
          }
          onMapIdle={ ()=> { console.log('map is ready') } }
          markers={this.state.markers}

        />
      </Col>
    );
  }
}

1 个答案:

答案 0 :(得分:1)

首先,您可以定义onChange地图处理程序:

<GoogleMap
 onChange={props.onMapChange}
/>

接下来添加到您的组件

<AsyncGoogleMap 
 onMapChange={this.handleMapChange}
/>

在您的AsyncGoogleMapComponent中,您将收到地图

的新参数
  handleMapChange = ({ center, zoom, bounds }) => {
    ....
    this.updateMarkers();
  };
相关问题