I am trying to use Google Map in React Component and it doesn't seem to work. I am currently referring to https://developers.google.com/maps/documentation/javascript/adding-a-google-map
Here is the code that I have for my component:
class ContactBody extends React.Component {
componentWillMount() {
const script = document.createElement("script");
const API = 'AIzaSyDbAz1XXxDoKSU2nZXec89rcHPxgkvVoiw';
script.src = `https://maps.googleapis.com/maps/api/js?key=${API}&callback=initMap`;
script.async = true;
document.body.appendChild(script);
};
initMap() {
const uluru = {lat: -25.363, lng: 131.044};
const map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: uluru
});
const marker = new google.maps.Marker({
position: uluru,
map: map
});
}
render() {
this.initMap();
return (
<div>
<h1>Contact</h1>
<div id="map" style={{width: 400, height: 300}}></div>
</div>
)
}
}
ReactDOM.render(
<ContactBody />,
document.getElementById('react')
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="react"></div>
However, when I run this, I am getting "Uncaught ReferenceError: google is not defined"
Could anyone tell me what is wrong with my code?
Thank you.
https://maps.googleapis.com/maps/api/js?key=${API}&callback=initMap;to yourindex.htmlfile?