addPolygonGeoFence method

Stream<GeoFenceEvent> addPolygonGeoFence({
  1. required List<LatLng> pointList,
  2. String customId = '',
  3. List<GeoFenceActiveAction> activeActions = const [GeoFenceActiveAction.In, GeoFenceActiveAction.Out, GeoFenceActiveAction.Stayed],
})

创建多边形电子围栏

Implementation

Stream<GeoFenceEvent> addPolygonGeoFence({
  required List<LatLng> pointList,
  String customId = '',
  List<GeoFenceActiveAction> activeActions = const [
    GeoFenceActiveAction.In,
    GeoFenceActiveAction.Out,
    GeoFenceActiveAction.Stayed,
  ],
}) async* {
  _geoFenceEventController ??= StreamController<GeoFenceEvent>.broadcast();

  final latitudeList = pointList.map((e) => e.latitude).toList();
  final longitudeList = pointList.map((e) => e.longitude).toList();

  if (Platform.isAndroid) {
    final context = await android_app_Application.get();
    _androidGeoFenceClient ??= await com_amap_api_fence_GeoFenceClient
        .create__android_content_Context(context);

    final _pointList = await com_amap_api_location_DPoint
        .create_batch__double__double(latitudeList, longitudeList);

    await _androidGeoFenceClient?.addPolygonGeoFence(
      polygon: _pointList,
      customId: customId,
      activeAction: activeActions.getActiveAction(),
    );
  } else if (Platform.isIOS) {
    _iosGeoFenceClient ??= await AMapGeoFenceManager.create__();
    _iosGeoFenceDelegate ??= await AMapGeoFenceManagerDelegate.anonymous__();

    await _iosGeoFenceClient?.set_delegate(
      _iosGeoFenceDelegate!
        ..amapGeoFenceManager_didGeoFencesStatusChangedForRegion_customID_error =
            (_, region, customId, error) async {
          final status = await region!.get_fenceStatus();
          _geoFenceEventController?.add(
            GeoFenceEvent(
              customId: customId,
              fenceId: await region.get_identifier(),
              status: GeoFenceStatusX.fromIOS(status!),
              genFence: GeoFence.ios(region),
            ),
          );
        },
    );

    await _iosGeoFenceClient
        ?.set_activeActionX(activeActions.getActiveAction());
    await _iosGeoFenceClient?.set_allowsBackgroundLocationUpdates(true);

    final _pointList = await CLLocationCoordinate2D.create_batch(
        latitudeList, longitudeList);

    await _iosGeoFenceClient
        ?.addPolygonRegionForMonitoringWithCoordinates_count_customID(
            _pointList, _pointList.length, customId);
  } else {
    throw '未实现的平台';
  }

  yield* _geoFenceEventController!.stream;
}