【问题标题】:How to set center using external lat, lng?如何使用外部 lat、lng 设置中心?
【发布时间】:2018-04-28 00:23:57
【问题描述】:

我已经构建了一个从数据库中提取地址的应用程序。我使用地理编码器 (https://www.npmjs.com/package/geocoder) 来获取地址的纬度和经度,现在希望每次我点击一个拉取地址的按钮时,它都会更新地图以缩放到该地址和中心。如果我将地址拉到地图组件外部,可以这样做吗?

到目前为止,这是我的地图组件:

    const MapWithASearchBox = compose(
  withProps({
    googleMapURL: "https://maps.googleapis.com/maps/api/js?key=AIzaSyDYwerCGhOnkM1sNHmSrckp8D1o9hY3mZ4&v=3.exp&libraries=geometry,drawing,places",
    loadingElement: <div style={{ height: `100%` }} />,
    containerElement: <div style={{ height: `400px` }} />,
    mapElement: <div style={{ height: `100%` }} />,
  }),
  lifecycle({
    componentDidMount(){

    },
    componentWillMount() {
      const refs = {}

      this.setState({

        bounds: null,
        center: {
          lat: -35.015742, lng: 138.520858
        },
        markers: [],
        onMapMounted: ref => {
          refs.map = ref;
        },
        onIdle: () => {
          this.setState({
            center: fullPos,
          })
        },
        onBoundsChanged: () => {
          this.setState({
            bounds: refs.map.getBounds(),
            center: refs.map.getCenter(),
          })
        },
        onSearchBoxMounted: ref => {
          console.log("s. mounted");
          refs.searchBox = ref;
        },
        onPlacesChanged: () => {
          console.log("places changed");
          console.log(refs.searchBox)
          console.log(refs)
          const places = refs.searchBox.getPlaces();
          console.log(places);
          const bounds = new google.maps.LatLngBounds();

          places.forEach(place => {
            if (place.geometry.viewport) {
              bounds.union(place.geometry.viewport)
            } else {
              bounds.extend(place.geometry.location)
            }
          });
          const nextMarkers = places.map(place => ({
            position: place.geometry.location,
          }));
          const nextCenter = _.get(nextMarkers, '0.position', this.state.center);

          this.setState({
            center: nextCenter,
            markers: nextMarkers,
          });
          // refs.map.fitBounds(bounds);
        },

      })
    },
  }),
  withScriptjs,
  withGoogleMap
)(props =>
  <GoogleMap
    ref={props.onMapMounted}
    defaultZoom={20}
    center={props.center}
    onBoundsChanged={props.onBoundsChanged}
    mapTypeId={google.maps.MapTypeId.SATELLITE}
    panControl={true}
    scrollwheel={false}
  >
    <SearchBox
      ref={props.onSearchBoxMounted}
      bounds={props.bounds}
      controlPosition={google.maps.ControlPosition.TOP_LEFT}
      onPlacesChanged={props.onPlacesChanged}
    >
      <input
        type="text"
        placeholder="Customized your placeholder"
        value={fullAdd}
        style={{
          boxSizing: `border-box`,
          border: `1px solid transparent`,
          width: `240px`,
          height: `32px`,
          marginTop: `27px`,
          padding: `0 12px`,
          borderRadius: `3px`,
          boxShadow: `0 2px 6px rgba(0, 0, 0, 0.3)`,
          fontSize: `14px`,
          outline: `none`,
          textOverflow: `ellipses`,
        }}
      />
    </SearchBox>
    {props.isMarkerShown && (
      <Marker position={fullPos} />
    )}
  </GoogleMap>
);

fullPos 是 lat、lng 变量,我在不同领域一直在使用,希望它能起作用。

【问题讨论】:

    标签: javascript reactjs google-maps google-maps-api-3


    【解决方案1】:

    拉取地图组件外部的地址

    不确定您的意思是将组件外部的新地址作为道具传递给它那么,也许您可​​以使用ComponentWillUpdate并做出一些漂亮的条件(以避免坏无限循环)并且只检查passedAddressProp是否改变,那么也许你可以做类似的事情:this.setState({center: passedAddressProp})

    很可能很明显,你的意思是我在离这个组件超远的地方得到了这个地址,所以他们没有机会交流,那么也许解决方案在于 redux/mobx(无论你喜欢哪个/使用) 并且当地址改变时,你可以用新地址触发一个动作,这个组件监听这部分状态,在componentWillUpdate内部也有一些漂亮的条件,你可以在这个全局时更改this.state.center部分状态发生变化。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多