Javascript React.js-React google地图事件返回

Javascript React.js-React google地图事件返回,javascript,google-maps,reactjs,Javascript,Google Maps,Reactjs,我在react中使用react google maps包,但我很难从组件中获取onDrag事件。我的代码如下: import React,{Component}来自'React'; 从“./GoogleAddressAutocomplete.js”导入GoogleAddressAutocomplete; 从“反应谷歌地图”导入{withGoogleMap,GoogleMap,Marker}; 从“axios”导入axios; 从“./NavWrapper.js”导入NavWrapper; 类M

我在react中使用react google maps包,但我很难从组件中获取onDrag事件。我的代码如下:

import React,{Component}来自'React';
从“./GoogleAddressAutocomplete.js”导入GoogleAddressAutocomplete;
从“反应谷歌地图”导入{withGoogleMap,GoogleMap,Marker};
从“axios”导入axios;
从“./NavWrapper.js”导入NavWrapper;
类MapPage扩展组件{
建造师(道具){
超级(道具);
此.state={
标记:{
职位:{
拉脱维亚:48.723627,
液化天然气:21.25419900003,
},
键:Date.now(),
默认动画:2,
},
地图中心:{lat:48.723627,lng:21.254199000003},
访问\u令牌:“”,
地址:''
}
}
handlempclick=this.handlempclick.bind(this);
handleMapDrag=this.handleMapDrag.bind(this);
handleMapLoad=this.handleMapLoad.bind(this);
handleMapClick(事件){
让那=这;
这是我的国家({
标记:{
位置:event.latLng,
默认动画:2,
关键字:Date.now()
},
地图中心:event.latLng
});
}
handleAddressChange(latLngObject,地址){
console.log('addr:=>'+地址);
}
handleMapDrag(事件){
console.log(事件);
}
handleMapLoad(事件){
console.log(事件);
}
render(){
const GoogleMapWrapper=withGoogleMap(道具=>(
));
返回(

) } }
导出默认映射页面您是否尝试为GoogleMap组件提供onDragStart?

返回以下内容,而不是返回该内容:

return (
        <div className="row-100">
            <NavWrapper/>
            <GoogleAddressAutocomplete addressChange={this.handleAddressChange.bind(this)} address={this.state.address}/>
            <br />
            <GoogleMapWrapper
                containerElement={
                    <div style={{ height: `50vh` }} />
                }
                mapElement={
                    <div style={{ height: `50vh` }} />
                }
                onMapClick={this.handleMapClick}
                onMapDrag={this.handleMapDrag}
                onMapLoad={this.handleMapLoad}
                markers={this.state.markers}
                center={this.state.mapCenter}
            />
        </div>
    )
给构造器

不太确定,我没有与React一起工作过很多次,但我认为这些可能是问题所在


希望有帮助。

因此解决方案是在实际实例上调用lat&lng函数,请注意GoogleMaps对象上的“ref”属性,这对于正确的函数很重要

import React,{Component}来自'React';
从“./GoogleAddressAutocomplete.js”导入GoogleAddressAutocomplete;
从“反应谷歌地图”导入{withGoogleMap,GoogleMap,Marker};
从“axios”导入axios;
从“./NavWrapper.js”导入NavWrapper;
类MapPage扩展组件{
建造师(道具){
超级(道具);
此.state={
标记:{
职位:{
拉脱维亚:48.723627,
液化天然气:21.25419900003,
},
键:Date.now(),
默认动画:2,
},
地图中心:{lat:48.723627,lng:21.254199000003},
访问\u令牌:“”,
地址:'',
mapRef:null
}
}
组件willmount(){
}
handlempclick=this.handlempclick.bind(this);
handleMapDrag=this.handleMapDrag.bind(this);
handleMapLoad=this.handleMapLoad.bind(this);
handleMapClick(事件){
让那=这;
这是我的国家({
标记:{
位置:event.latLng,
默认动画:2,
关键字:Date.now()
},
地图中心:event.latLng
});
}
handleAddressChange(latLngObject,地址){
console.log('addr:=>'+地址);
}
handleMapDrag(){
设mapRef=this.\u mapponent;
log(mapRef.getCenter().lat()+';'+mapRef.getCenter().lng());
}
手推车(地图){
这是.\u mapComponent=map;
}
render(){
const GoogleMapWrapper=withGoogleMap(道具=>(
));
返回(

) } }
导出默认映射页面GoogleMap组件确实触发了它的事件绑定到的函数,因此拖动地图会导致控制台日志,但我无法检索到当前地图中心的GPS坐标,这基本上是相同的绑定方式。无论如何,我找到了一个解决方案,我会发布一个答案,谢谢大家的参与;)如果您不介意,您知道如何使用此软件包更改缩放控制位置吗。
handleMapClick = this.handleMapClick.bind(this);
handleMapDrag = this.handleMapDrag.bind(this);
handleMapLoad = this.handleMapLoad.bind(this);