【问题标题】:Reposition the center of the map when the location changes?当位置改变时重新定位地图的中心?
【发布时间】:2019-02-14 23:55:41
【问题描述】:

大家好,我正在使用react-google-maps 库。每次我的位置发生变化时,我都会尝试重新定位我的地图(缩放标记所在的位置),但我对如何实现整个事情有点迷茫。我可以看到标记正在更新,但地图仍停留在其defaultCenter 位置。

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import {
  GoogleMap,
  Marker,
  withScriptjs,
  withGoogleMap
} from 'react-google-maps';

import environment from '../../config/environment';

class Map extends Component {
  static propTypes = {
    defaultZoom: PropTypes.number,
    center: PropTypes.shape({
      lat: PropTypes.number,
      lng: PropTypes.number
    }),
    location: PropTypes.shape({
      lat: PropTypes.number,
      lng: PropTypes.number
    }),
    onPositionChanged: PropTypes.func
  };

  static defaultProps = {
    defaultZoom: 14,
    center: {
      lat: 60.1699,
      lng: 24.9384
    },
    location: {},
    onPositionChanged: () => {}
  };

  constructor(props) {
    super(props);
    this.mapRef = React.createRef((ref) => {
      this.mapRef = ref;
    });
  }

  componenDidUpdate() {
    console.log(`I'm about to update with props: ${JSON.strongify(prevProps, undefined, 2)}`);
  }

  onPositionChanged = (location) => {
    console.log(`This the new location onPositionChange:${JSON.stringify(location, undefined, 2)}`);
    const newLocation = new window.google.maps.LatLng(location.lat, location.lng);
    // [NOTE]: try using the panTo() from googleMaps to recenter the map ? but don't know how to call it.

    return (
      <Marker
        position={newLocation}
      />
    );
  }

  render() {
    const {
      center,
      defaultZoom,
      location,
      onPositionChanged
    } = this.props;

    return (

      <GoogleMap
        className="google-map"
        onClick={onPositionChanged(location)}
        defaultZoom={defaultZoom}
        defaultCenter={center}
        ref={this.mapRef}
      >

        {/* <Marker
          position={location}
        /> */}

        { this.onPositionChanged(location) }
      </GoogleMap>
    );
  }
}

const SchedulerGoogleMap = withScriptjs(withGoogleMap(Map));
const SchedulerMap = props => (
  <SchedulerGoogleMap
    googleMapURL={`https://maps.googleapis.com/maps/api/js?key=${
      environment.GOOGLE_MAPS_API_KEY
    }&v=3`}
    loadingElement={<div style={{ height: '20vh' }} />}
    containerElement={<div style={{ height: '100%' }} />}
    mapElement={<div style={{ height: '20vh', width: '100%' }} />}
    {...props}
  />
);


export { Map, SchedulerMap, SchedulerGoogleMap };

【问题讨论】:

  • 分享codesandbox,我们可以使用fitBounds

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


【解决方案1】:

只需将center 属性传递给您的GoogleMap 组件,而不是defaultCenter 属性。 center 属性是可变的,而 defaultZoom is not

【讨论】:

  • 代码示例:&lt;GoogleMap zoom={zoom} center={center}&gt;
  • 谢谢!我想你的意思是defaultCenter 不是defaultZoom
【解决方案2】:

这似乎对我有用,以防万一其他人遇到同样的问题。

... ommited_code

class Map extends Component {

... ommited_code

  componentDidUpdate(prevProps) {
    if (prevProps.location !== this.props.location) {
      this.mapRef.panTo(
        new window.google.maps.LatLng(this.props.location.lat, this.props.location.lng)
      );
    }
  }

  render() {
    const {
      center,
      defaultZoom,
      location
    } = this.props;

    return (

      <GoogleMap
        className="google-map"
        defaultZoom={defaultZoom}
        defaultCenter={center}
        ref={(ref) => {
          this.mapRef = ref;
        }}
      >
        <Marker position={new window.google.maps.LatLng(location.lat, location.lng)} />
      </GoogleMap>
    );
  }
}

...ommited_code

【讨论】:

    【解决方案3】:

    panTo() 是 google.maps.Map 类的一个方法。 (https://developers.google.com/maps/documentation/javascript/reference/map#Map.panTo)

    这似乎是您正在寻找的功能,因此您需要通过引用您为地图设置的类名在您的谷歌地图上调用它,然后将您创建的LatLang 对象提供给panTo 方法:

    window.google.maps.Map(document.getElementsByClassName("google-map")).panTo(newLocation);

    【讨论】:

    • 刚刚尝试过,但是当我尝试在onPositionChanged 方法上设置它时,它给了我一个cannot find defaultView 错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-08
    • 1970-01-01
    • 1970-01-01
    • 2012-10-18
    • 1970-01-01
    • 2012-01-23
    相关资源
    最近更新 更多