Javascript (反应)在谷歌地图上打开模式标记单击
更新:添加了Dandy,如果我将其绑定到componentDidMount,我现在可以访问componentDidMount中的this.setState函数,但由于this.setState在componentDidMount中,this.setState不是函数Javascript (反应)在谷歌地图上打开模式标记单击,javascript,google-maps,reactjs,ecmascript-6,Javascript,Google Maps,Reactjs,Ecmascript 6,更新:添加了Dandy,如果我将其绑定到componentDidMount,我现在可以访问componentDidMount中的this.setState函数,但由于this.setState在componentDidMount中,this.setState不是函数 marker.addListener('click', function() { infowindow.open(map, marker); this.setState({ showModal
marker.addListener('click', function() {
infowindow.open(map, marker);
this.setState({ showModal: true });
});
我想我必须在componentDidMount之外构建地图,但我不确定从哪里开始
marker.addListener('click', function() {
infowindow.open(map, marker);
this.setState({ showModal: true });
});
原件:
marker.addListener('click', function() {
infowindow.open(map, marker);
this.setState({ showModal: true });
});
所以我在React中构建了一个谷歌地图,我想在点击标记时打开一个模式
marker.addListener('click', function() {
infowindow.open(map, marker);
this.setState({ showModal: true });
});
我的渲染和
marker.addListener('click', function() {
infowindow.open(map, marker);
this.setState({ showModal: true });
});
在componentDidMount中,但我发现我无法更改componentDidMount中的状态。。。关于如何让我的模态显示在marker click上,有什么建议吗
marker.addListener('click', function() {
infowindow.open(map, marker);
this.setState({ showModal: true });
});
从“React”导入React;
从“反应路由器”导入{Link};
进口{
一行
上校,
FormGroup,
控制标签,
FormControl,
按钮
网格,
情态动词
爆米花,
工具提示,
覆盖式起重工
}来自“反应引导”;
导出类映射扩展React.Component{
建造师(道具){
超级(道具);
用户位置:{lat:40.7128,lng:-74.0059},
默认中心:{
纬度:40.7128,
液化天然气:-74.0059
},
showModal:错,
};
this.close=this.close.bind(this);
this.open=this.open.bind(this);
this.componentDidMount=this.componentDidMount.bind(this);
}
关闭(){
this.setState({showmodel:false});
}
开(){
this.setState({showmodel:true});
}
componentDidMount(){
//这个.getCurrentPosition();
var contentString=''+
''+
''+
“纽约”+
''+
“内容字符串”+
marker.addListener('click', function() {
infowindow.open(map, marker);
this.setState({ showModal: true });
});
''+
'';
设sourland={lat:40.473927,lng:-74.694482};
var map=new google.maps.map(document.getElementById('map'){
缩放:12,
中心:{
纬度:40.7128,
液化天然气:-74.0059
}
});
var infowindow=new google.maps.infowindow({
content:contentString,
最大宽度:200
});
var marker=new google.maps.marker({
职位:sourland,
地图:地图,
标题:“乌卢鲁(艾尔斯岩)”
});
//模态码
marker.addListener('click',function()){
信息窗口。打开(地图、标记);
this.setState({showmodel:true});
});
}
render(){
console.log(this.state);
常数popover=(
非常流行,这样的订婚
);
常量工具提示=(
哇!
);
返回(
启动演示模式
Sourland山地保护区攀岩
Sourland山保护区
允许在空白处攀爬,但要注意空白处
marker.addListener('click', function() {
infowindow.open(map, marker);
this.setState({ showModal: true });
});
这是一件府绸
这里有一个
marker.addListener('click', function() {
infowindow.open(map, marker);
this.setState({ showModal: true });
});
模式中的工具提示
这里有一个
marker.addListener('click', function() {
infowindow.open(map, marker);
this.setState({ showModal: true });
});
接近
);
}
}
从您的代码来看,您似乎没有在构造函数中正确设置状态。因此,“showModal”的状态不可访问/不存在
marker.addListener('click', function() {
infowindow.open(map, marker);
this.setState({ showModal: true });
});
确保已将构造函数配置为设置组件的状态:
marker.addListener('click', function() {
infowindow.open(map, marker);
this.setState({ showModal: true });
});
更改:
marker.addListener('click', function() {
infowindow.open(map, marker);
this.setState({ showModal: true });
});
constructor(props) {
super(props);
userPosition: {lat: 40.7128, lng: -74.0059},
defaultCenter: {
lat: 40.7128,
lng: -74.0059
},
showModal: false,
};
为此:
marker.addListener('click', function() {
infowindow.open(map, marker);
this.setState({ showModal: true });
});
constructor(props) {
super(props);
this.state = {
userPosition: {lat: 40.7128, lng: -74.0059},
defaultCenter: {
lat: 40.7128,
lng: -74.0059
},
showModal: false
}
};
这应该确保使用
This.setState()
正确地将组件的状态从true更新为false 在实现了Dandy的构造函数修复之后,我使用post作为参考在ES6中重写了map组件
marker.addListener('click', function() {
infowindow.open(map, marker);
this.setState({ showModal: true });
});
因为Meteor 1.4应用程序中使用了这个组件,所以我必须使用严格的ES6(没有Andrews示例中的ES5或ES7+)
marker.addListener('click', function() {
infowindow.open(map, marker);
this.setState({ showModal: true });
});
导出类映射扩展React.Component{
建造师(道具){
超级(道具);
此.state={
用户位置:{lat:40.7128,lng:-74.0059},
默认中心:{
纬度:40.7128,
液化天然气:-74.0059
},
缩放:10,
showModal:错,
};
this.close=this.close.bind(this);
this.open=this.open.bind(this);
}
componentDidMount(){
this.map=this.createMap()
this.marker=this.createMarker()
this.infoWindow=this.createInfoWindow()
google.maps.event.addListener(this.map,‘zoom_changed’,()=>this.handleZoomChange())
google.maps.event.addListener(this.marker'click',()=>this.handleMarkerClick())
}
componentDidUnMount(){
google.maps.event.clearListeners(映射“zoom\u changed”)
}
createMap(){
让mapOptions={
zoom:this.state.zoom,
中心:this.mapCenter()
}
返回新的google.maps.Map(this.refs.mapCanvas,mapOptions)
}
地图中心(){
返回新的google.maps.LatLng(
this.state.defaultCenter.lat,
this.state.defaultCenter.lng
)
}
createMarker(){
返回新的google.maps.Marker({
位置:this.mapCenter(),
地图:这是地图
})
}
createInfoWindow(){
let contentString=“我是一个包含信息的窗口”
返回新的google.maps.InfoWindow({
map:this.map,
主持人:这个,马克,
内容:contentString
})
}
handleMarkerClick(){
控制台日志(“ow”);
这是我的国家({
showModal:对
});
this.infowindow.open(地图、标记);
}
handleZoomChange(){
这是我的国家({
zoom:this.map.getZoom()
})
}
关闭(){
this.setState({showmodel:false});
}
开(){
this.setState({showmodel:true});
}
render(){
常数popover=(
非常流行,这样的订婚
);
常量工具提示=(
哇!
);
返回
当前缩放:{this.state.Zoom}
marker.addListener('click', function() {
infowindow.open(map, marker);
this.setState({ showModal: true });
});