React Google Maps中带有搜索框的地图很慢?

时间:2019-05-31 19:31:25

标签: reactjs google-maps

我想将https://tomchentw.github.io/react-google-maps/#searchbox上的示例改编为具有地面覆盖层的地图,以便该地图同时具有搜索框和地面覆盖层;到目前为止,我的工作是在https://github.com/khpeek/beomaps/tree/searchbox。这是组件:

/* global google */

import React from 'react'
import _ from 'lodash'
import { compose, withProps, lifecycle } from 'recompose'
import {
  withScriptjs,
  withGoogleMap,
  GoogleMap,
  Marker,
  GroundOverlay } from 'react-google-maps'
const { SearchBox } = require("react-google-maps/lib/components/places/SearchBox");

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

      this.setState({
        bounds: null,
        center: {
          lat: 41.9, lng: -87.624
        },
        markers: [],
        onMapMounted: ref => {
          refs.map = ref;
        },
        onBoundsChanged: () => {
          this.setState({
            bounds: refs.map.getBounds(),
            center: refs.map.getCenter(),
          })
        },
        onSearchBoxMounted: ref => {
          refs.searchBox = ref;
        },
        onPlacesChanged: () => {
          const places = refs.searchBox.getPlaces();
          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={15}
    center={props.center}
    onBoundsChanged={props.onBoundsChanged}
  >
    <SearchBox
      ref={props.onSearchBoxMounted}
      bounds={props.bounds}
      controlPosition={google.maps.ControlPosition.TOP_LEFT}
      onPlacesChanged={props.onPlacesChanged}
    >
      <input
        type="text"
        placeholder="Customized your placeholder"
        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.markers.map((marker, index) =>
      <Marker key={index} position={marker.position} />
    )}
  </GoogleMap>
);

问题在于,在带有搜索框的地图中,我无法像在带有叠加层的原始地图中那样平滑地平移。这是在此GIF中显示的(我只能将其压缩为8种颜色以适应StackOverflow的2 MB文件大小限制):

enter image description here

从GIF中可以看到,在MapWithASearchBox中,我只能平移一小段距离,然后“停止”,而在下面的地图中,我可以正常平移。是什么原因造成的?

1 个答案:

答案 0 :(得分:1)

不要使用属性“ onBoundsChanged”来设置状态,而应使用“ onIdle”,因为这种调用不那么频繁。

请给https://github.com/tuccle22作答(https://github.com/tomchentw/react-google-maps/issues/777