searchDriveRoute method

Future<DriveRouteResult> searchDriveRoute ({@required LatLng from, @required LatLng to, List<LatLng> passedByPoints: const [], String avoidRoad: '' })

驾车出行路线规划

指定起点from和终点to, 并指定途经点passedByPoints和避开道路名称avoidRoad进行搜索

Implementation

static Future<DriveRouteResult> searchDriveRoute({
  @required LatLng from,
  @required LatLng to,
  List<LatLng> passedByPoints = const [],
  String avoidRoad = '',
}) async {
  // 会在listener中关闭
  // ignore: close_sinks
  final _controller = StreamController<DriveRouteResult>(sync: true);

  platform(
    android: (pool) async {
      // 起点
      final fromLatLng = await ObjectFactory_Android
          .createcom_amap_api_services_core_LatLonPoint__double__double(
        from.latitude,
        from.longitude,
      );
      // 终点
      final toLatLng = await ObjectFactory_Android
          .createcom_amap_api_services_core_LatLonPoint__double__double(
        to.latitude,
        to.longitude,
      );

      // 起终点
      final fromAndTo = await ObjectFactory_Android
          .createcom_amap_api_services_route_RouteSearch_FromAndTo__com_amap_api_services_core_LatLonPoint__com_amap_api_services_core_LatLonPoint(
              fromLatLng, toLatLng);

      // 途经点
      final List<com_amap_api_services_core_LatLonPoint> passby = [];
      for (var item in passedByPoints) {
        passby.add(await ObjectFactory_Android
            .createcom_amap_api_services_core_LatLonPoint__double__double(
          item.latitude,
          item.longitude,
        ));
      }

      // 创建请求对象
      final query = await ObjectFactory_Android
          .createcom_amap_api_services_route_RouteSearch_DriveRouteQuery__com_amap_api_services_route_RouteSearch_FromAndTo__int__com_amap_api_services_core_LatLonPoint__com_amap_api_services_core_LatLonPoint__String(
        fromAndTo,
        0,
        passby,
        [], // 暂不支持多维数组
        avoidRoad,
      );

      // 获取android上下文
      final context = await ObjectFactory_Android.getandroid_app_Activity();

      // 创建搜索对象
      _androidRouteSearch = await ObjectFactory_Android
          .createcom_amap_api_services_route_RouteSearch__android_content_Context(
              context);

      // 设置回调
      await _androidRouteSearch
          .setRouteSearchListener(_AndroidSearchListener(_controller));

      // 开始搜索
      await _androidRouteSearch.calculateDriveRouteAsyn(query);

      // 局部变量从HEAP中解除引用
      pool
        ..add(fromLatLng)
        ..add(toLatLng)
        ..add(fromAndTo)
        ..addAll(passby)
        ..add(query);
    },
    ios: (pool) async {
      _iosSearch = await ObjectFactory_iOS.createAMapSearchAPI();

      // 创建起点
      final fromLatLng = await ObjectFactory_iOS.createAMapGeoPoint();
      await fromLatLng.set_latitude(from.latitude);
      await fromLatLng.set_longitude(from.longitude);
      // 创建终点
      final toLatLng = await ObjectFactory_iOS.createAMapGeoPoint();
      await toLatLng.set_latitude(to.latitude);
      await toLatLng.set_longitude(to.longitude);

      // 设置回调
      await _iosSearch.set_delegate(_IOSSearchListener(_controller));

      // 创建搜索请求
      final request =
          await ObjectFactory_iOS.createAMapDrivingRouteSearchRequest();
      // 设置起点
      await request.set_origin(fromLatLng);
      // 设置终点
      await request.set_destination(toLatLng);
      // 设置避开道路
      await request.set_avoidroad(avoidRoad);
      // 策略 默认0 速度优先
      await request.set_strategy(0);
      // 设置途经点
      final List<AMapGeoPoint> passby = [];
      for (var item in passedByPoints) {
        final geoPoint = await ObjectFactory_iOS.createAMapGeoPoint();
        await geoPoint.set_latitude(item.latitude);
        await geoPoint.set_longitude(item.longitude);
        passby.add(geoPoint);
      }
      await request.set_waypoints(passby);
      // 暂不支持避开区域
//        await request.set_avoidpolygons([]);

      // 开始搜索
      await _iosSearch.AMapDrivingRouteSearch(request);

      // 局部变量从HEAP中解除引用
      pool
        ..add(fromLatLng)
        ..add(toLatLng)
        ..addAll(passby)
        ..add(request);
    },
  );
  return _controller.stream.first;
}