react-google-maps: map is not displaying in browser; no error in console
I am trying to integrate google maps in my application; I created a component just like you provided and called it with a route but it doesnt shows map as I needed.
My Map Component:
import {default as React, Component} from "react";
import {default as update} from "react-addons-update";
import {default as canUseDOM} from "can-use-dom";
import {default as _} from "lodash";
import {GoogleMapLoader, GoogleMap, Marker} from "react-google-maps";
import {triggerEvent} from "react-google-maps/lib/utils";
class Mapping extends Component {
state = {
markers: [{
position: {
lat: 25.0112183,
lng: 121.52067570000001,
},
key: "Taiwan",
defaultAnimation: 2
}]
};
constructor (props, context) {
super(props, context);
this.handleWindowResize = _.throttle(this.handleWindowResize, 500);
}
componentDidMount () {
if (!canUseDOM) {
return;
}
window.addEventListener("resize", this.handleWindowResize);
}
componentWillUnmount () {
if (!canUseDOM) {
return;
}
window.removeEventListener("resize", this.handleWindowResize);
}
handleWindowResize () {
console.log("handleWindowResize", this._googleMapComponent);
triggerEvent(this._googleMapComponent, "resize");
}
/ *
* This is called when you click on the map.
* Go and try click now.
*/
handleMapClick (event) {
var {markers} = this.state;
markers = update(markers, {
$push: [
{
position: event.latLng,
defaultAnimation: 2,
key: Date.now()// Add a key property for: http://fb.me/react-warning-keys
},
],
});
this.setState({ markers });
if (3 === markers.length) {
this.props.toast(
"Right click on the marker to remove it",
"Also check the code!"
);
}
}
handleMarkerRightclick (index, event) {
/*
* All you modify is data, and the view is driven by data.
* This is so called data-driven-development. (And yes, it's now in
* web front end and even with google maps API.)
*/
var {markers} = this.state;
markers = update(markers, {
$splice: [
[index, 1]
],
});
this.setState({ markers });
}
render () {
return (
<GoogleMapLoader
containerElement={
<div
{...this.props}
style={{
height: "100%"
}}
/>
}
googleMapElement={
<GoogleMap
ref={(map) => (this._googleMapComponent = map) && console.log(map.getZoom())}
defaultZoom={3}
defaultCenter={{lat: -25.363882, lng: 131.044922}}
onClick={this.handleMapClick}>
{this.state.markers.map((marker, index) => {
return (
<Marker
{...marker}
onRightclick={this.handleMarkerRightclick.bind(this, index)} />
);
})}
</GoogleMap>
}
/>
);
}
}
export default Mapping;
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 4
- Comments: 53 (9 by maintainers)
i found out that converting height and width’s values from percentage to pixels worked for me.
I have a similar issue with seeing the google logo and only a grey box.
But the interesting thing is that the map appears when I resize the window!
Is that happening with anyone else?
the issue is not related to the react-google-map. the container element of the map needs to have an height specified. it will not render anything if height is set to a percentage. so you could either set the containerElement a fix height or the better solution would be wrap the map component inside another div and give it a fix height.so your render looks something like this
and in your styles
{width: 200, height: 200}results in an empty grey box. Clicking or dragging on the box generatesUncaught TypeError: Cannot read property 'x' of undefinedSorry I should of posted an update here. I got it to work properly by placing it within another div that was
position: absolute;and hadwidthandheightset to100%, and then additionally setting the style on thecontainerElementprop toheight: 100%;. Without the outer div, the component completely breaks.I did this and can now see the google logo and terms of use, but the map area is grey. Also clicking or dragging within the component triggers errors and breaks the component
Had a similar problem. Adding zoom & center props helped in my case.
Just to complement, I was having an issue with Safari not rendering the map (It was working fine in Chrome). I had a
min-height: 850pxin the outer div. I then changed it toheight: 850pxand worked.Later on, I wanted to make it proportional to the screen size and changed to
height: 100%, but it didn’t work on Safari again (good on Chrome though).Finally, I changed to
height: 100vh, and worked on both browsers 👍@Climber22 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/api
Our Spectrum community: https://spectrum.chat/react-google-maps Our Slack channel: https://join.slack.com/t/react-google-maps-api/shared_invite/enQtODc5ODU1NTY5MzQ4LTBiNTYzZmY1YmVjYzJhZThkMGU0YzUwZjJkNGJmYjk4YjQyYjZhMDk2YThlZGEzNDc0M2RhNjBmMWE4ZTJiMjQ
Enjoy! https://localhost:8000 AIzaSyAIEIUwCGFxj7LQoJP7f1uIjrdW_caZ5dg
http://localhost:3000 AIzaSyDGoMq24FaPFSVxKgMeXE5fkJw0JkdQ7i8 https://react-google-maps-api-gatsby-demo.netlify.com/ AIzaSyC3kkfcd1XTail_ft8YEoNto49NKOmFhWo
http://localhost:9001 AIzaSyCakXXwZ_CyPggaQBrYmbw5QhgGUcoMD_E
export const googleMapKey = ‘AIzaSyCakXXwZ_CyPggaQBrYmbw5QhgGUcoMD_E’ export default googleMapKey http://localhost:6060 AIzaSyCgkbHxXpGlShz9Doj6dhbnXR59v-G8bO0
I was having this problem and after much frustration, I figured out that the coordinates I was testing it with had the lat and lng flipped. Guess I was sending it unmappable coordinates. Just another possible solution to throw into the mix here. You can see what was rendering for me below with the bad coordinates…
I’m having the same issue as @zkdzegede. Also, when I resize the page, the center is completely off.
Anyone have a possible solution ?
Google maps API will initialize on the DOM defined by
containerElement. You have to make sure thecontainerElementwill have height & width when rendered on the page.I also encountered a similar issue. But it’s all gray, there is no google logo or any other stuff. No errors. It turned out I use the map in two different component, one of them passing “zoom” property from parent but the other doesn’t. Once I fixed this and it worked. Help this could help.
Add style={{width: ‘100%’, height: ‘100%’}} to all the parents elements of google map, including body and html.
I had so much trouble getting the map to show and none of the answers above worked for me, so I’m posting this solution hoping it would solve y’all hours of playing around!
I think because I’m using a dialog and a map in that dialog I had to do some extra steps:
This is my dialog in HTML
<div id="modal-create" class="modal fade" role="dialog">in that I have the map
<div id="map"></div>for a css style I have this
#map{ height: 300px; }change the height to whatever you want, for me it helped to have a set px height and not a %I added a dom listener in my doucment.ready
google.maps.event.addDomListener(window, 'load', initMap);and my initMap() is as follows
really hope this works for you whomever you are ❤️
This works:
style={{width: '100%', height: '100%'}}I was also having the same issue as @zkdzegede. Fixed it by adding default width and height to the containerElement prop of the ScriptLoader component which I’m using:
I got mine working by fiddling with its containers. Can’t remember exactly what I did but I think it needed an outer container with position absolute and a width and height set