Skip to content

Map not loading (blank screen) #279

@HsuTimothy

Description

@HsuTimothy

I realize this question has popped up before but none of the solutions mentioned in those issues seem to have resolved mine.

I've literally copied and pasted the example code from https://www.fullstackreact.com/articles/how-to-write-a-google-maps-react-component/#the-map-container-component (changing the api key of course) but still can't get the simple maps to load.

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

export class MapContainer extends Component {
  render() {
    const style = { width: "100vw", height: "100vh" };
    return (
      <div style={style}>
        <Map google={this.props.google} />
      </div>
    );
  }
}

export default GoogleApiWrapper({
  apiKey: "MY_API_KEY"
})(MapContainer);
import React, { Component } from "react";
import ReactDOM from "react-dom";

class Map extends Component {
  componentDidMount() {
    this.loadMap();
  }
  componentDidUpdate(prevProps, prevState) {
    if (prevProps.google !== this.props.google) {
      this.loadMap();
    }
  }

  loadMap() {
    if (this.props && this.props.google) {
      // google is available
      const { google } = this.props;
      const maps = google.maps;

      const mapRef = this.refs.map;
      const node = ReactDOM.findDOMNode(mapRef);

      let zoom = 14;
      let lat = 37.774929;
      let lng = -122.419416;
      const center = new maps.LatLng(lat, lng);
      const mapConfig = Object.assign({}, {
        center: center,
        zoom: zoom
      })
      this.map = new maps.Map(node, mapConfig);
    }
    // ...
  }

  render() {
    return <div ref="map">Loading map...</div>;
  }
}

export default Map;

Would greatly appreciate any tips!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions