admin管理员组文章数量:1025465
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 itthis.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
2 Answers
Reset to default 2if 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)
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 itthis.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
2 Answers
Reset to default 2if 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)
本文标签:
版权声明:本文标题:javascript - Google Maps "Uncaught TypeError: Cannot read property 'defaultView' of undefined" 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745630391a2160127.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论