【问题标题】:React-google-maps +redux - how to center map when new location is fetched?React-google-maps +redux - 获取新位置时如何使地图居中?
【发布时间】:2017-11-16 07:12:27
【问题描述】:

我正在使用 react-google-maps 并遵循该示例:LINK。我正在获取来自 GPS 设备的数据,我想在获取新数据时居中。

这是整个地图组件的代码:

const GettingStartedGoogleMap = withGoogleMap(props => (

  <GoogleMap
    ref={props.onMapLoad}
    defaultZoom={18}
    defaultCenter={props.defCenter}
    onClick={props.onMapClick}
  >
    {props.markers.map((marker,i) => (
      <Marker
        key={i}
        position={marker.location}
        time={marker.time}
        onClick={() => props.onMarkerClick(marker)}
      >
        { marker.isShown &&
          <InfoWindow onCloseClick={() => props.onMarkerClick(marker)}>
             <div className="marker-text">{marker.time} </div>
          </InfoWindow>
        }
      </Marker>

    ))}
  </GoogleMap>
));

class GettingStartedExample extends Component {

  componentDidMount(){
    this.props.fetchMarkers();
    console.log("MONT");
    console.log(this.props.markers.length);

  }
  componentWillReceiveProps(){
    console.log(this.props.markers);
    console.log(this.props.markers.length);
  }

  state = {
     markers: this.props.markers,
     center: {lat: 50.07074, lng: 19.915718},
   };

  handleMapLoad = this.handleMapLoad.bind(this);
  handleMarkerClick = this.handleMarkerClick.bind(this);

  handleMapLoad(map) {
    this._mapComponent = map;
    if (map) {
      console.log(map.getZoom());
    }
  }

  handleMarkerClick(targetMarker) {
    /*
     * All you modify is data, and the view is driven by data.
     * This is so called data-driven-development. (And yes, it's now in
     * web front end and even with google maps API.)
     */
    const nextMarkers = this.state.markers.filter(marker => marker !== targetMarker);
    if(targetMarker.isShown==true)
    {
        targetMarker.isShown=false;
    }
    else
    {
        targetMarker.isShown=true;
    }
    this.setState({
      markers: nextMarkers,
      center: {lat: 5.07074, lng: 19.915718}
    });
  }

  render() {
    const markers = this.props.markers.map((marker,i) => {
      return (
          <Marker key={i} position={marker.location} time={marker.time} onClick={this.handleMarkerClick} />
        )});
    var centerPos;
    var lastMark;
    if(markers[markers.length-1]!== undefined)
        {
          centerPos=markers[markers.length-1].props.position;
          lastMark=markers[markers.length-1].props;
          console.log(centerPos);
        }
    else {
          centerPos={lat: 5.07074, lng: 19.915718};
        }
    return (
      <div className='container map-container'>
        <GettingStartedGoogleMap
          containerElement={
            <div style={{ height: `100%` }} />
          }
          mapElement={
            <div style={{ height: `100%` }} />
          }
          onMapLoad={this.handleMapLoad}
          markers={this.props.markers}
          onMarkerClick={this.handleMarkerClick}
          defCenter={this.state.center}
          lastMarker={lastMark}
        />
      </div>
    );
  }
}

GettingStartedExample.propTypes={
  markers: React.PropTypes.array.isRequired,
  fetchMarkers: React.PropTypes.func.isRequired
}

function mapStateToProps(state){
  return{
    markers:state.markers
  }
}

export default connect(mapStateToProps,{fetchMarkers})(GettingStartedExample);

可能我必须将中心状态设置为 this.props.markers 中的最后一个对象,我尝试在 componentWillReceiveProps() 上执行此操作,但我不知道该怎么做。我也在 render() 中尝试过:

if(markers[markers.length-1]!== undefined)
        {
          centerPos=markers[markers.length-1].props.position;
          lastMark=markers[markers.length-1].props;
          console.log(centerPos);
        }
    else {
          centerPos={lat: 5.07074, lng: 19.915718};
        }

并试图将它传递给状态,但它没有工作。我确信解决方案很简单,但我没有使用 React 的经验。如果有帮助,我将添加 actions.js 的代码,其中定义了用于获取标记的 redux 操作:

export const SET_MARKERS='SET_MARKERS';

/*markers*/
export function setMarkers(markers){
  return{
    type:SET_MARKERS,
    markers
  }
}

export function fetchMarkers(){
  return dispatch => {
    fetch('/api/markers')
      .then(res=>res.json())
      .then(data=>dispatch(setMarkers(data.markers)));
    ;
  }
}

【问题讨论】:

    标签: google-maps reactjs redux react-redux fetch


    【解决方案1】:

    只需将 center 属性添加到 GoogleMap 组件。

    <GoogleMap 
        center={props.center}
    />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-18
      • 2021-02-19
      • 1970-01-01
      • 2021-10-20
      • 2018-07-13
      • 1970-01-01
      • 1970-01-01
      • 2020-03-10
      相关资源
      最近更新 更多