GoogleMap refs undefined when using GoogleMapLoader
This console.log() will return undefined:
class MyMap extends Component {
componentDidMount() {
console.log(this._mymap);
}
render() {
return (
<GoogleMapLoader
containerElement={
<div
{...this.props}
style={{
height: "100%",
}}
/>
}
googleMapElement={
<GoogleMap
ref={(map) => this._mymap = map}
defaultZoom={3}
defaultCenter={{lat: -25.363882, lng: 131.044922}}>
</GoogleMap>
}
/>
);
}
}
Is there a way to save a map reference at all? It works fine if you don't use GoogleMapLoader, but as soon as you nest it, references to the map will always be undefined and cannot be saved.
If you need a reference of map object, You can try like this: ref={googleMap => { if (!googleMap) { return ; } console.log(googleMap); this.gmap = googleMap; } } this.gmap.props.map will give you the reference to map .
The issue was that I was doing an initial map render and then updating it with Ajax. That caused issues with the ref on componentDidMount(). I moved my logic to componentDidUpdate() to properly get a hold of the map ref.
Glad it's resolved! We're also looking for maintainers. Involve in #266 to help strengthen our community!
Can I have multiple markers in this like below, There is no error in the console but still Ia m not able to show the google map -
import React, {PropTypes} from 'react';
import {GoogleMapLoader, GoogleMap, Marker} from 'react-google-maps';
import _ from 'lodash';
class MapContainer extends React.Component {
render() {
return (
<section className="searchInput-MapScreen">
<GoogleMapLoader
containerElement={
<div
style={{
height: `100%`
}}
/>
}
googleMapElement={
<GoogleMap
defaultZoom={10}
//defaultCenter={{ lat: latitude, lng: longitude }}
>
{_.map(this.props.areas, (a, i) => {
let lat = parseFloat(a.lat.replace('"','').replace('"',''));
let lon = parseFloat(a.lon.replace('"','').replace('"',''));
console.log(lat)
console.log(lon)
return (
<Marker key={i}
position= {{ lat : lat, lng : lon }}
defaultAnimation={2}
/>
)
})}
</GoogleMap>
}
/>
</section>
)
}
}
MapContainer.propTypes = {
areas: PropTypes.array
}
export default MapContainer
Please refer to Getting Help section in the README (or #469).
I'm getting undefined google error I don't know why as I've included all libraries
// External Dependencies
import React, { Component } from 'react';
import { withGoogleMap, GoogleMap , GoogleMapLoader,Marker} from 'react-google-maps';
// Internal Dependencies
import './style.css';
class HelloWorld extends Component {
static slug = 'nel_hello_world';
render() {
var google = window.google;
const GoogleMapExample = withGoogleMap(props => (
<GoogleMap
defaultCenter = { { lat: 40.756795, lng: -73.954298 } }
defaultZoom = { 13 }
>
</GoogleMap>
));
// const Content = this.props.content;
return(
<div>
<GoogleMapExample
containerElement={ <div style={{ height: `500px`, width: '500px' }} /> }
mapElement={ <div style={{ height: `100%` }} /> }
/>
</div>
);
}
}
export default HelloWorld;
@naveed520 The repo of this project is unmaintained more than a year, and we had build new version https://www.npmjs.com/package/@react-google-maps/api
We had rewrite it to TypeScript, and updating it frequently: https://github.com/JustFly1984/react-google-maps-api/tree/master/packages/react-google-maps-api You can enjoy autocomplete.
You can see our docs: https://react-google-maps-api-docs.netlify.com/
Also a lot of examples: https://react-google-maps-api-gatsby-demo.netlify.com/ https://github.com/JustFly1984/react-google-maps-api/tree/master/packages/react-google-maps-api-gatsby-example/src/examples
The bundle size is much smaller: https://bundlephobia.com/result?p=@react-google-maps/[email protected]
Enjoy!