如何在使用DirectionsRenderer时更改点之间的strokeColor

时间:2018-04-21 06:25:29

标签: reactjs google-maps google-maps-api-3 react-google-maps

我使用react-google-maps在网站中渲染地图。在使用DirectionsService的航点时,我对每一行的strokeColor都有一个问题。如何在它们之间更改strokeColor以及在使用DirectionsService之后如何回调。这是我同源的例子:

  

https://tomchentw.github.io/react-google-maps/#directionsrenderer

 let DirectionsService = new google.maps.DirectionsService;
 var directionsDisplay = new google.maps.DirectionsRenderer;
DirectionsService.route(
    {
        origin: new google.maps.LatLng(10.8441402, 106.76757429999999),
        destination: new google.maps.LatLng(10.8463797,106.7721405),
        travelMode: google.maps.TravelMode.DRIVING,
        optimizeWaypoints: true,


        waypoints: [
             {
                 location: new google.maps.LatLng(10.8454946,106.764759),
                 stopover : false
             }
         ]
    },
    (result, status) => {
        console.log(result);
        if (status === google.maps.DirectionsStatus.OK) {
             this.setState({directions: result})
            return (typeof callback == 'function') && callback( result);
        } else {
            console.error(`error fetching directions`, result);
        }
    }
);

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

为了在组件中访问DirestionsRendererOptions,你需要使用options prop,就像那样:

<DirectionsRenderer
  directions={props.directions}
  options={{
      polylineOptions: {
          strokeOpacity: 0.5,
          strokeColor: '#FF0000',
      },

  }}
/>

请参阅文档以了解有关您有权访问的选项的更多信息:https://developers.google.com/maps/documentation/javascript/reference/3.exp/directions#DirectionsRendererOptions