ReactJS компонент карт Google не будет отображаться - PullRequest
0 голосов
/ 03 июня 2018

Я пишу веб-приложение, которое позволяет вам ввести адрес и отображает карту с указаниями от данного адреса до 4 других (пока жестко запрограммированных) адресов.Все работает нормально, пока я не хочу ввести новый адрес и обновить картукод:

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import registerServiceWorker from './registerServiceWorker';
import { compose, withProps, withPropsOnChange, withState, lifecycle } from     "recompose";
import {
withScriptjs,
withGoogleMap,
GoogleMap,
Marker,
DirectionsRenderer,
} from "react-google-maps";

const MapWithADirectionsRenderer = compose(
withProps({
    googleMapURL: "https://maps.googleapis.com/maps/api/js?key=AIzaSyC4R6AN7SmujjPUIGKdyao2Kqitzr1kiRg&v=3.exp&libraries=geometry,drawing,places",
    loadingElement: <div style={{ height: `100%` }} />,
    containerElement: <div style={{ height: `500px` }} />,
    mapElement: <div style={{ height: `100%` }} />,
}),        
withScriptjs,
withGoogleMap,
lifecycle({
    componentDidMount() {
        this.setState({
            newDirections: [],
        });

        let destinations = ['Imperial College', '123 Aldersgate Street', '17 Moorgate', 'Spitafields Market']

        const DirectionsService = new google.maps.DirectionsService();

        DirectionsService.route({
            origin: this.props.origin,
            destination: destinations[0],
            travelMode: google.maps.TravelMode.TRANSIT,
        }, (result, status) => {
            if (status === google.maps.DirectionsStatus.OK) {
                this.setState({
                    directions: result,
                    newDirections: this.state.newDirections.concat([result])
                });
            } else {
                console.error(`error fetching directions ${result}`);
            }
        });

        DirectionsService.route({
            origin: this.props.origin,
            destination: destinations[1],
            travelMode: google.maps.TravelMode.TRANSIT,
        }, (result, status) => {
            if (status === google.maps.DirectionsStatus.OK) {
                this.setState({
                    directions1: result,
                    newDirections: this.state.newDirections.concat([result])
                });
            } else {
                console.error(`error fetching directions ${result}`);
            }
        });

        DirectionsService.route({
            origin: this.props.origin,
            destination: destinations[2],
            travelMode: google.maps.TravelMode.TRANSIT,
        }, (result, status) => {
            if (status === google.maps.DirectionsStatus.OK) {
                this.setState({
                    directions2: result,
                    newDirections: this.state.newDirections.concat([result])
                });
            } else {
                console.error(`error fetching directions ${result}`);
            }
        });

        DirectionsService.route({
            origin: this.props.origin,
            destination: destinations[3],
            travelMode: google.maps.TravelMode.TRANSIT,
        }, (result, status) => {
            if (status === google.maps.DirectionsStatus.OK) {
                this.setState({
                    directions3: result,
                    newDirections: this.state.newDirections.concat([result])
                });
            } else {
                console.error(`error fetching directions ${result}`);
            }
        });
    }      
})
)(props =>
<GoogleMap
    defaultZoom={7}
    defaultCenter={new google.maps.LatLng(51.435341, -0.131116)}
>
    {props.directions && <DirectionsRenderer directions={props.newDirections[0]} />}
    {props.directions1 && <DirectionsRenderer directions={props.newDirections[1]} />}
    {props.directions2 && <DirectionsRenderer directions={props.newDirections[2]} />}
    {props.directions3 && <DirectionsRenderer directions={props.newDirections[3]} />}

</GoogleMap>
);

class AddressForm extends React.Component {
constructor(props) {
    super(props);
    this.state = { value: '' };

    this.handleChange = this.handleChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
}

handleChange(event) {
    this.setState({ value: event.target.value });
}

handleSubmit(event) {

    let targetAddress = this.state.value;
    ReactDOM.render(<MapWithADirectionsRenderer origin={targetAddress} destination='Imperial College' />, document.getElementById('mapplace'));
    event.preventDefault();

}

render() {
    return (
        <form onSubmit={this.handleSubmit}>
            <label>
                Address:
                <input type="text" value={this.state.value} onChange={this.handleChange} />
            </label>
            <input type="submit" value="submit" />
        </form>
    );
}
}

registerServiceWorker();


ReactDOM.render(<AddressForm />, document.getElementById('formplace'));

При загрузке пользователь видит только визуализированный компонент.После ввода адреса и отправки формы карта отображается правильно.Однако, когда я ввожу новый адрес и повторно отправляю форму, карта не перерисовывается.

Что-то мне не хватает?

1 Ответ

0 голосов
/ 12 июня 2018

Возможно, вы ищете функцию componentDidUpdate, которая запускается после любого обновления, componentDidMount будет вызываться только один раз после первого рендеринга.

Я бы предложилследующий список изменений:

a) ввести отдельную функцию для рисования маршрутов:

 drawRoutes() {
        let destinations = ['Imperial College', '123 Aldersgate Street', '17 Moorgate', 'Spitafields Market']
        const DirectionsService = new google.maps.DirectionsService();
        DirectionsService.route({
            origin: this.props.origin,
            destination: destinations[0],
            travelMode: google.maps.TravelMode.TRANSIT,
        }, (result, status) => {
            if (status === google.maps.DirectionsStatus.OK) {
                this.setState({
                    directions: result,
                    newDirections: this.state.newDirections.concat([result])
                });
            } else {
                console.error(`error fetching directions ${result}`);
            }
        });

        DirectionsService.route({
            origin: this.props.origin,
            destination: destinations[1],
            travelMode: google.maps.TravelMode.TRANSIT,
        }, (result, status) => {
            if (status === google.maps.DirectionsStatus.OK) {
                this.setState({
                    directions1: result,
                    newDirections: this.state.newDirections.concat([result])
                });
            } else {
                console.error(`error fetching directions ${result}`);
            }
        });

        DirectionsService.route({
            origin: this.props.origin,
            destination: destinations[2],
            travelMode: google.maps.TravelMode.TRANSIT,
        }, (result, status) => {
            if (status === google.maps.DirectionsStatus.OK) {
                this.setState({
                    directions2: result,
                    newDirections: this.state.newDirections.concat([result])
                });
            } else {
                console.error(`error fetching directions ${result}`);
            }
        });

        DirectionsService.route({
            origin: this.props.origin,
            destination: destinations[3],
            travelMode: google.maps.TravelMode.TRANSIT,
        }, (result, status) => {
            if (status === google.maps.DirectionsStatus.OK) {
                this.setState({
                    directions3: result,
                    newDirections: this.state.newDirections.concat([result])
                });
            } else {
                console.error(`error fetching directions ${result}`);
            }
        });
    }

b) установить начальное состояние:

  componentWillMount() {
       this.setState({
                newDirections: [],
       });
   }

c) нарисовать маршруты:

componentDidMount() {
        this.drawRoutes();
}

d) перерисовать маршруты, если адрес был изменен

componentDidUpdate(prevProps, prevState) {
     if (prevProps.origin !== this.props.origin) {
          this.setState({
              directions: null,
              directions1: null,
              directions2: null,
              directions3: null,
              newDirections: []
         });
         this.drawRoutes();
     }
}

Демо

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...