Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OnClick do not re-render the map #513

Open
alikucukavci opened this issue May 6, 2021 · 0 comments
Open

OnClick do not re-render the map #513

alikucukavci opened this issue May 6, 2021 · 0 comments

Comments

@alikucukavci
Copy link

Hi,

I am trying to run google-map-react in my application.
The idea is when I open the page with google maps it should show me a heatmap with all the data. Then I have a dropdown list where I can pick data based on the weekday. So for example if I pick monday the google map should rerender the map and show me only data for mondays. The map is rendered correctly at the beginning but when I click the button in the dropdown list it doesnt rerender.

Is this a bug or am I doing something wrong? Hope that anyone can guide me here.

I have shared my code below. The first code shows the state I am passing to Maps component. The second code is the Maps component.

FIRST CODE

import React, { useState, useEffect } from 'react'
import Map from '../map/Map'
import data from '../../assets/data/heatmapData'
import Scatter from "../scatter-plot/Scatter";
import Dropdown from 'react-bootstrap/Dropdown';
import DropdownButton from 'react-bootstrap/DropdownButton';

import './content-section.css'

const ContentSection = () => {
  const [heatmapData, setHeatmapData] = useState(data);
  // useEffect(() => {
  //   handleWeekday()
  // }, [])

  const handleWeekday = (input) => 
  {
    console.log(input);
    const heatmapMondayData = [{ lat: 37.782551, lng: -122.44536800000003 },
    { lat: 37.782745, lng: -122.44458600000002 },
    { lat: 37.782842, lng: -122.44368800000001 },
    { lat: 37.782919, lng: -122.442815 }];

    setHeatmapData(heatmapMondayData ); 
  }

  return (
  <>
    <div className="container">
      <div className="header-text">This is a header</div>
      <Scatter />
      <Map center={{ lat: 37.775, lng: -122.434 }} zoom={13} positions={heatmapData} />
      <DropdownButton id="dropdown-basic-button" title="Dropdown button">
        <Dropdown.Item onClick={handleMonday}>Monday</Dropdown.Item>
 
      </DropdownButton>
    </div>
  </>
  )
}

export default ContentSection

SECOND CODE


import React, { Component } from "react";
import { Map, HeatMap, GoogleApiWrapper } from "google-maps-react";
import './map.css'

const gradient = [
    "rgba(0, 255, 255, 0)",
    "rgba(0, 255, 255, 1)",
    "rgba(0, 191, 255, 1)",
    "rgba(0, 127, 255, 1)",
    "rgba(0, 63, 255, 1)",
    "rgba(0, 0, 255, 1)",
    "rgba(0, 0, 223, 1)",
    "rgba(0, 0, 191, 1)",
    "rgba(0, 0, 159, 1)",
    "rgba(0, 0, 127, 1)",
    "rgba(63, 0, 91, 1)",
    "rgba(127, 0, 63, 1)",
    "rgba(191, 0, 31, 1)",
    "rgba(255, 0, 0, 1)"
  ];
  class MapContainer extends React.Component {
    render() {
      console.log(this.props)

      useEffect(() => {
        this.props.positions
      }, [])
      return (
            <div className="map-container">
                <Map
                google={this.props.google}
                zoom={this.props.zoom}
                initialCenter={this.props.center}
                >
                    <HeatMap
                    gradient={gradient}
                    positions={this.props.positions}
                    opacity={1}
                    radius={20}
                    />
          </Map>

          <div>{this.props.positions[0].lat}</div>

            </div>
      );
    }
  }
  
  export default GoogleApiWrapper({
    apiKey: "XXX",
    libraries: ["visualization"]
  })(MapContainer);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant