Google maps 此值在react组件中被Google maps覆盖
我的组件代码如下:Google maps 此值在react组件中被Google maps覆盖,google-maps,reactjs,Google Maps,Reactjs,我的组件代码如下: export default class map extends React.Component { drwmap() { let mapOptions = { zoom: zoom, mapTypeId: google.maps.MapTypeId.ROADMAP, center: new google.maps.LatLng(this.lat, this.lng),
export default class map extends React.Component {
drwmap() {
let mapOptions = {
zoom: zoom,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: new google.maps.LatLng(this.lat, this.lng),
mapTypeControl: true,
mapTypeControlOptions: {
position: google.maps.ControlPosition.LEFT_BOTTOM
},
streetViewControl: true,
minZoom: 2,
};
// here also this.lat and this.lng are not avilable
this.map = new google.maps.Map(document.getElementById('mapdiv'), mapOptions);
}
loadmap(event) {
geocoder.geocode({'address': this.place}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
let lat = results[0].geometry.location.lat();
let lng = results[0].geometry.location.lng();
// here this is window looks google map modified scope and I dont have access to this of react
this.lat = lat;
this.lng = lng;
this.drwmap();
}
});
}
componentDidMount() {
this.loadmap();
}
}
当我在reactjs中使用谷歌地图进行地理编码时,我得到了这个窗口值。谷歌地图似乎正在用其他值覆盖它,react中的范围不可用
如何使其可用,以便在组件中使用公共变量?我使用react中的箭头函数解决了此问题
this.geocoder.geocode({'address': this.place},(results, status) => {
/ / here this is available as component
});
我使用react中的箭头函数解决了这个问题
this.geocoder.geocode({'address': this.place},(results, status) => {
/ / here this is available as component
});
我已经重新格式化了您的代码示例,使其具有更清晰的块嵌套。尽可能多地清理代码示例始终是一个好主意,因为如果代码示例易于阅读,您就更有可能让人花时间阅读并尝试回答您的问题。我已重新格式化了您的代码示例,使其具有更清晰的块嵌套。尽可能多地清理代码样本始终是一个好主意,因为如果代码样本易于阅读,那么您就更有可能让人们花时间阅读并尝试回答您的问题。