最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Google Maps "Uncaught TypeError: Cannot read property 'defaultView' of undefined"

matteradmin6PV0评论

I keep getting this error in when trying to load a google map in React:

"Uncaught TypeError: Cannot read property 'defaultView' of undefined"

I have this ponent which loads a Google map but it's failing and I can't figure out where it's going wrong. I believe the error is confined to this ponent because the app renders successfully but the map doesn't display inside it because of the above error.

class DoctorsMap extends React.Component {
  constructor(props) {
    super(props);
    this.geocoder = new google.maps.Geocoder();
    this.mapRef = React.createRef();
    this.handleMarkerClick = this.handleMarkerClick.bind(this);
    this.setTheCenter = this.setTheCenter.bind(this);
  }

  ponentDidMount () {
    this.map = new google.maps.Map(this.mapRef.current.outerHTML);
    this.setTheCenter();
    this.markerManager = new MarkerManager(this.map, this.handleMarkerClick);
    this.markerManager.updateMarkers(this.props.doctors);
  }

  ponentDidUpdate () {
    return this.markerManager.updateMarkers(this.props.doctors);
  }

  setTheCenter() {
    this.geocoder.geocode({address: `${this.props.address}`}, (results, status) => {
      if (status === google.maps.GeocoderStatus.OK) {
        const coordinates = results[0].geometry.location;
        this.map.setCenter(coordinates);
        this.map.setZoom(11);
      } else {
        alert("Geocode failed: " + status);
      }
    });
  }

  handleMarkerClick(doctor) {
    return this.props.history.push(`/doctor/${doctor.id}`);
  }

  docSearchToggle() {
    if (this.props.location.pathname.includes("/doctor/")) {
      return ["map-doc-canvas", "map-doc-container"];
    } else {
      return ["map-search-canvas", "map-search-container"];
    }
  }

  render () {
    return(
      <div id={this.docSearchToggle()[1]}>
        <div id={this.docSearchToggle()[0]} ref={this.mapRef}></div>
      </div>
    );
  }
}

Any ideas?

I keep getting this error in when trying to load a google map in React:

"Uncaught TypeError: Cannot read property 'defaultView' of undefined"

I have this ponent which loads a Google map but it's failing and I can't figure out where it's going wrong. I believe the error is confined to this ponent because the app renders successfully but the map doesn't display inside it because of the above error.

class DoctorsMap extends React.Component {
  constructor(props) {
    super(props);
    this.geocoder = new google.maps.Geocoder();
    this.mapRef = React.createRef();
    this.handleMarkerClick = this.handleMarkerClick.bind(this);
    this.setTheCenter = this.setTheCenter.bind(this);
  }

  ponentDidMount () {
    this.map = new google.maps.Map(this.mapRef.current.outerHTML);
    this.setTheCenter();
    this.markerManager = new MarkerManager(this.map, this.handleMarkerClick);
    this.markerManager.updateMarkers(this.props.doctors);
  }

  ponentDidUpdate () {
    return this.markerManager.updateMarkers(this.props.doctors);
  }

  setTheCenter() {
    this.geocoder.geocode({address: `${this.props.address}`}, (results, status) => {
      if (status === google.maps.GeocoderStatus.OK) {
        const coordinates = results[0].geometry.location;
        this.map.setCenter(coordinates);
        this.map.setZoom(11);
      } else {
        alert("Geocode failed: " + status);
      }
    });
  }

  handleMarkerClick(doctor) {
    return this.props.history.push(`/doctor/${doctor.id}`);
  }

  docSearchToggle() {
    if (this.props.location.pathname.includes("/doctor/")) {
      return ["map-doc-canvas", "map-doc-container"];
    } else {
      return ["map-search-canvas", "map-search-container"];
    }
  }

  render () {
    return(
      <div id={this.docSearchToggle()[1]}>
        <div id={this.docSearchToggle()[0]} ref={this.mapRef}></div>
      </div>
    );
  }
}

Any ideas?

Share Improve this question asked Jul 31, 2018 at 22:10 mwjohnson324mwjohnson324 211 gold badge1 silver badge4 bronze badges 1
  • 2 Nevermind, figured out the problem. The problem was this line: ponentDidMount () { this.map = new google.maps.Map(this.mapRef.current.outerHTML); I needed to pass it this.mapRef.current. Instead of passing in an html element I was passing a string representation of it. – mwjohnson324 Commented Jul 31, 2018 at 23:08
Add a ment  | 

2 Answers 2

Reset to default 2

if below code it help to your issue

If your are using Sapui5 use this code

sap.ui.getCore().byId("Your Id").getDomRef();

OR

this.getView().byId("Your ID").getDomRef();

If you are using HTML

document.getElementById("Your Id").getDomRef();

Just new google.maps.Map(this.mapRef.current). require a HTMLElement but you support a string(HTMLElement.outerHTML)

Post a comment

comment list (0)

  1. No comments so far