tomchentw / react-google-maps

React.js Google Maps integration component
https://tomchentw.github.io/react-google-maps/
MIT License
4.62k stars 939 forks source link

Search bar makes panning of the map sluggish #987

Open khpeek opened 5 years ago

khpeek commented 5 years ago

In this branch, https://github.com/khpeek/beomaps/tree/searchbox, I've essentially copied the example from https://tomchentw.github.io/react-google-maps/#searchbox above another map which has just a GroundOverlay.

What I notice, as shown in this GIF (https://imgur.com/gallery/HoZC3jX), is that in the map with the search bar, I can only pan so far before the map 'halts'. What is causing this 'performance bug'?

Here is the MapWithASearchBox component:

/* 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>
);
JustFly1984 commented 5 years ago

Hello @khpeek

This library is unsupported and unmaintained more than a year. Please look at our re-implementation @react-google-maps/api library. We have working docs and examples, spectrum community and several maintainers. We have get rid of all extra dependencies, and had rewritten the codebase to typescript. We have easier API and supporting new React versions.

https://github.com/JustFly1984/react-google-maps-api/tree/master/packages/react-google-maps-api

https://www.npmjs.com/package/@react-google-maps/api https://react-google-maps-api-docs.netlify.com/ https://react-google-maps-api-gatsby-demo.netlify.com/ https://spectrum.chat/react-google-maps?tab=posts