1

プロジェクトに react-google-maps を追加すると、render が 2 回機能しました。だから私は2つの円を1つずつ持っています。また、onDragEnd()メソッドで中心座標を表示します。このイベントは、このサークルの 1 つだけで機能します。

他の Google マップはプロジェクトに存在しません。

これを修正しようとしていたいくつかの方法を次に示します。1) withGoogleMap のみを使用する、2) 親コンポーネントの render() メソッド内で GoogleMapsWrapper コンポーネントを使用する、3) componentDidMount(); を使用する。satckoverflow からすべてを試してみてください:)そして何も役に立ちません。

import React, { Component } from 'react';
import MapForm from './mapForm';
import { GoogleMap, withGoogleMap, withScriptjs, Circle } from 'react-google-maps';

const GoogleMapsWrapper = withScriptjs(withGoogleMap(props => {
    const {onMapMounted, ...otherProps} = props;
    return <GoogleMap {...otherProps} ref={c => {
       onMapMounted && onMapMounted(c)
    }}>{props.children}</GoogleMap>
}));

class GoogleMapsContainer extends Component {
    state = {
        coords: {lat:0, lng: 0}
    };


dragCircle = () => {
    this.setState({
        coords: {
            lat: this._circle.getCenter().lat(),
            lng: this._circle.getCenter().lng()
        }
    })
}

render() {
    return(
        <div style={{display: 'flex',flexDirection: 'row', width: '100%', marginLeft: '37px'}}>
        <MapForm 
            filters={this.props.filters}
            coords={this.state.coords}  
        />
        <GoogleMapsWrapper
            googleMapURL={`https://maps.googleapis.com/maps/api/js?key=${KEY}&v=3.exp&libraries=geometry,drawing,places`}
            loadingElement={<div style={{height: `100%`}}/>}
            containerElement={<div style={{position: 'relative',width: '100%',  }} />}
            mapElement={<div style={{height: `100%`}}/>}
            defaultZoom={13}
            defaultCenter={KYIV}
        >
            <Circle
                ref={(circle) => {this._circle = circle}}
                defaultCenter = {KYIV}
                defaultDraggable={true}
                defaultEditable={true}
                defaultRadius={2000}
                onDragEnd = {this.dragCircle}
                options={{
                    strokeColor: `${colors.vividblue}`,
                    fillColor: `${colors.vividblue}`,
                    fillOpacity: 0.1
                }}
            />
      </GoogleMapsWrapper>
      </div>
    )
  }
}

export default GoogleMapsContainer;

私の方法で必要な円は 1 つだけです。マイサークル

4

1 に答える 1

0

OK、問題はプロジェクトの React StrictMode コンポーネントにありました。

于 2019-04-05T07:06:57.127 に答える