React Google Maps:在MapWithADirectionRenderer上显示Markers重构Component

时间:2018-05-28 19:55:19

标签: javascript reactjs google-maps react-google-maps recompose

我的组件 MapWithADirectionsRenderer 定义为here

var markers = [];
const MapWithADirectionsRenderer = compose(

withProps({
    googleMapURL: "https://maps.googleapis.com/maps/api/js?...",
    loadingElement: <div style={{ height: `100%` }} />,
    containerElement: <div style={{ height: `100%` }} />,
    mapElement: <div style={{ height: `100%` }}/>
  }),
  withScriptjs,
  withGoogleMap,
  lifecycle({

  componentDidMount() {

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

  DirectionsService.route({
    origin: this.props.origin,
    destination: this.props.destination,
    travelMode: google.maps.TravelMode.DRIVING,
  }, (result, status) => {

    if (status === google.maps.DirectionsStatus.OK) {

      // here I fill my marker array correctly

      this.setState({
          directions: result
      });

     } else {
      console.error(`error fetching directions ${result}`);
     }
   });
 }

 })
)(props=> 
  <GoogleMap
    defaultZoom={3}
    defaultCenter={// coordinates}
  >
    {props.directions && <DirectionsRenderer 
                            options={{draggable:true}}
                            directions={props.directions}
                            ref={(r) => directionsRef = r}
                            onDirectionsChanged={getDirections}
                             />
    }
    {markers.map( (marker,i) => (
                              <Marker
                                key={i}
                                position={{ lat: marker.lat, lng: marker.lng }}
                            >
                              </Marker>
                          ))}


  </GoogleMap>
);

但在地图上移动路线后,从Google更新的路线正确更新,但不是我想要显示的标记。始终显示相同的标记。

有没有办法更新 MapWithADirectionRenderer ,以便重新渲染我的标记而不仅仅是方向?谢谢

0 个答案:

没有答案