You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Flutter的openrouteservice包中设置avoid_polygons

在Flutter中使用openrouteservice API避开多边形区域规划路线

要实现避开指定多边形区域的导航路线,你需要利用openrouteservice API的避障参数,将多边形坐标转换为符合库要求的格式后传入directionsMultiRouteCoordsPost函数。具体操作如下:

1. 转换多边形坐标格式

你现有的多边形坐标是LatLng类型,需要转换成openrouteservice库使用的ORSCoordinate类型:

// 转换避开区域的多边形坐标
final List<ORSCoordinate> avoidPolygon = [
  ORSCoordinate(latitude: 8.923613, longitude: 125.575063),
  ORSCoordinate(latitude: 8.920441, longitude: 125.573503),
  ORSCoordinate(latitude: 8.923704, longitude: 125.567126),
  ORSCoordinate(latitude: 8.926735, longitude: 125.568404),
  // 可选:添加首坐标闭合多边形,部分场景下可提升API识别精度
  ORSCoordinate(latitude: 8.923613, longitude: 125.575063),
];

2. 在路线请求中传入避障参数

修改你现有的directionsMultiRouteCoordsPost调用,添加avoidPolygons参数(对应openrouteservice API的avoid_polygons字段):

final List<ORSCoordinate> routeCoordinates2 = await openrouteservice.directionsMultiRouteCoordsPost(
  coordinates: [
    ORSCoordinate(latitude: 8.920576, longitude: 125.576619),
    ORSCoordinate(
      latitude: endTappedLocation!.latitude,
      longitude: endTappedLocation!.longitude,
    ),
  ],
  // 添加避开多边形的参数
  avoidPolygons: [avoidPolygon],
);

注意事项

  • avoidPolygons参数接受一个多边形列表,若需避开多个区域,可传入多个ORSCoordinate数组。
  • 确保多边形坐标顺序正确(顺时针或逆时针均可),API会自动解析为闭合区域。
  • 若路线仍穿过目标区域,可适当扩大多边形坐标范围,调整避障区域的覆盖范围。

内容的提问来源于stack exchange,提问作者Kendaichi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 12:05:13