How i draw a route with react-google-maps component?

后端 未结 1 1602
有刺的猬
有刺的猬 2021-02-10 19:42

I\'m traying to draw a route between two points with react-google-maps but is not working for me. Can u help me with this problem? Here\'s a example of my react component.

相关标签:
1条回答
  • 2021-02-10 20:20

    To render a route Google Maps API provides Directions Service, in case of react-google-maps library DirectionsRenderer component is available which is a wrapper around DirectionsRenderer class which in turn:

    Renders directions obtained from the DirectionsService.

    Assuming the data for route is provided in the following format:

    const places = [
      {latitude: 25.8103146,longitude: -80.1751609},
      {latitude: 27.9947147,longitude: -82.5943645},
      {latitude: 28.4813018,longitude: -81.4387899},
      //...
    ]
    

    the following component could be introduced to calculate and render directions via react-google-maps library:

    class MapDirectionsRenderer extends React.Component {
      state = {
        directions: null,
        error: null
      };
    
      componentDidMount() {
        const { places, travelMode } = this.props;
    
        const waypoints = places.map(p =>({
            location: {lat: p.latitude, lng:p.longitude},
            stopover: true
        }))
        const origin = waypoints.shift().location;
        const destination = waypoints.pop().location;
    
    
    
        const directionsService = new google.maps.DirectionsService();
        directionsService.route(
          {
            origin: origin,
            destination: destination,
            travelMode: travelMode,
            waypoints: waypoints
          },
          (result, status) => {
            if (status === google.maps.DirectionsStatus.OK) {
              this.setState({
                directions: result
              });
            } else {
              this.setState({ error: result });
            }
          }
        );
      }
    
      render() {
        if (this.state.error) {
          return <h1>{this.state.error}</h1>;
        }
        return <DirectionsRenderer directions={this.state.directions} />;
      }
    }
    

    Here is a demo


    For React 16.8 or above, MapDirectionsRenderer could be implemented (using Hooks) as below:

    function MapDirectionsRenderer(props) {
      const [directions, setDirections] = useState(null);
      const [error, setError] = useState(null);
    
      useEffect(() => {
        const { places, travelMode } = props;
    
        const waypoints = places.map(p => ({
          location: { lat: p.latitude, lng: p.longitude },
          stopover: true
        }));
        const origin = waypoints.shift().location;
        const destination = waypoints.pop().location;
    
        const directionsService = new google.maps.DirectionsService();
        directionsService.route(
          {
            origin: origin,
            destination: destination,
            travelMode: travelMode,
            waypoints: waypoints
          },
          (result, status) => {
            console.log(result)
            if (status === google.maps.DirectionsStatus.OK) {
              setDirections(result);
            } else {
              setError(result);
            }
          }
        );
      });
    
      if (error) {
        return <h1>{error}</h1>;
      }
      return (
        directions && (
          <DirectionsRenderer directions={directions} />
        )
      );
    }
    
    0 讨论(0)
提交回复
热议问题