react-google-maps HeatmapLayer undefined

Viewed 2344

I wanna use HeatmapLayer from react-google-maps but I get the error blow:

Uncaught (in promise) TypeError: Cannot read property 'HeatmapLayer' of undefined

I've been triying to make this thing works but i couldn't hit the nail on the head, also looking in this link there's nothing helpful (https://github.com/tomchentw/react-google-maps/issues/409)

import {withGoogleMap,GoogleMap} from "react-google-maps"
import HeatmapLayer from "react-google-maps/lib/visualization/HeatmapLayer"

render(){

var data = [new window.google.maps.LatLng(37.782551, -122.445368),
            new window.google.maps.LatLng(37.782745, -122.444586),
            new window.google.maps.LatLng(37.782842, -122.443688),
            new window.google.maps.LatLng(37.782919, -122.442815),
            new window.google.maps.LatLng(37.782992, -122.442112),
            new window.google.maps.LatLng(37.783100, -122.441461)
      ]
  const Heatmap = withGoogleMap(props => (
<GoogleMap
    defaultZoom={1}
    center={{lat: 19.435031,lng: -99.167357}}
>
<HeatmapLayer data = {data} />
</GoogleMap>));

return(
  <div className="googleMap">
<Heatmap
  containerElement={
    <div style={{ height: `100%` }} />
  }
  mapElement={
    <div style={{ height: `100%` }} />
  }
  center={{ lat: -25.363882, lng: 131.044922 }}
/>
</div>)
}
}
3 Answers

Your import location is wrong I believe. It should be like below. More info here

import HeatmapLayer from "react-google-maps/lib/components/visualization/HeatmapLayer";

To be able to access visualization library in google maps react create you can do something like this

/*global google*/
import React, {
  Component
} from React;
import {
  GoogleApiWrapper,
  Map
} from "google-maps-react";

class YourComponent extends Component {
constructor(props){
super(props);
this.mapRef= React.createRef();
}

componentDidMount(){
    //you can access your visualization lib through this
    console.log(google.maps.visualization);
    
    //you can access you map reference through
    this.mapRef.current.map
}

  render() {
    return <Map ref={this.mapRef} { /*defaults for the map here*/ } >
      <
      /Map>
  }
}

export default GoogleApiWrapper((props) => ({
  apiKey: yourapikey,
  libraries: ["visualization"],
}))(YourComponent);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

Related