google-maps-api-3 - 在 react-google-maps 中添加航点

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

editable samplecode如何在以下代码中使用航点 航点是否有助于绘制我在数据库中更新的方式,无论 ponits 将基于我更新的点

const DirectionsService = new google.maps.DirectionsService();
                 const  DirectionsDisplay = new google.maps.DirectionsRenderer({suppressMarkers: true},{strokeColor:"#4a4a4a"});

      DirectionsService.route({

        origin: new google.maps.LatLng(this.state.orgin.latitude ,this.state.orgin.longitude),
         destination: new google.maps.LatLng(this.state.destination.latitude ,this.state.destination.longitude),
          travelMode: google.maps.TravelMode.DRIVING,

      }, 
       (result, status) => {
        if (status === google.maps.DirectionsStatus.OK) {
          this.setState({
            directions: result,
          });
        } else {
          console.error(`error fetching directions ${result}`);
        }
      });

            }).catch(function (err) {

            });

    }

  })

)(props =>
  <GoogleMap
    defaultZoom={50}>
     <DirectionsRenderer directions={props.directions}   />

  < Marker

  position={{  lat:props.delivery!=undefined?props.delivery.latitude:null, lng:  props.delivery!=undefined?props.delivery.longitude:null }} />

  </GoogleMap>

);
    return (
      <MapWithADirectionsRenderer />
    )
  }

最佳答案

您可以通过在您的路线请求中添加 waypoints[] 的 DirectionsWaypoint 数组来添加航点。

您可以查看此文档以了解更多信息:https://developers.google.com/maps/documentation/javascript/directions#DirectionsRequests

这是一个示例航点数组:

waypoints: [
    {
        location: new google.maps.LatLng(14.546748, 121.05455)
    },
    {
        location: new google.maps.LatLng(14.552444,121.044488)
    }
]

这是带有航点的示例方向请求:
DirectionsService.route({
   origin: new google.maps.LatLng(14.533593, 121.053128),
   destination: new google.maps.LatLng(14.550895, 121.025079),
   travelMode: google.maps.TravelMode.DRIVING,
   waypoints: [
        {
           location: new google.maps.LatLng(14.546748, 121.05455)
        },
        {
           location: new google.maps.LatLng(14.552444,121.044488)
        }
   ]
}, (result, status) => {
   if (status === google.maps.DirectionsStatus.OK) {
      this.setState({
         directions: result,
      });
   } else {
     console.error(`error fetching directions ${result}`);
   }
});

关于google-maps-api-3 - 在 react-google-maps 中添加航点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47513308/

相关文章:

javascript - 检测覆盖是否包含标记对象。 Google Maps API V3,绘图库

angular - 如何在 ionic 2 应用程序中自动更新谷歌地图上的当前位置

reactjs - react-google-maps 如何获取标记位置?

javascript - React-Google-Maps 显示两个标记,一个在原始位置,另一个标记跟随 map 的新中心 onDrag

google-maps - Google Maps API 中的其他投影?

javascript - Google map JS API 今天停止为我工作

javascript - 加载谷歌地图控件时是否有事件?

reactjs - 每次我点击标记时,我的谷歌地图都会刷新

reactjs - 如何使用 react-google-maps 禁用街景?

javascript - React js从路径中删除点更新多边形