1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
// ignore_for_file: non_constant_identifier_names
import 'dart:async';
import 'dart:io';
import 'package:amap_core_fluttify/amap_core_fluttify.dart';
import 'package:amap_location_fluttify/src/android/android.export.g.dart';
import 'package:amap_location_fluttify/src/ios/ios.export.g.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'enums.dart';
import 'extensions.dart';
import 'models.dart';
/// 高德定位 主类
class AmapLocation {
static AmapLocation instance = AmapLocation._();
AmapLocation._() {
initAndroidListener();
}
com_amap_api_location_AMapLocationClient? _androidClient;
com_amap_api_fence_GeoFenceClient? _androidGeoFenceClient;
AMapLocationManager? _iosClient;
AMapGeoFenceManager? _iosGeoFenceClient;
AMapGeoFenceManagerDelegate? _iosGeoFenceDelegate;
AMapLocationManagerDelegate? _iosLocationDelegate;
StreamController<Location>? _locationController;
StreamController<GeoFenceEvent>? _geoFenceEventController;
/// 初始化
Future<void> init({required String iosKey}) {
return platform(
android: (pool) async {
// 获取上下文, 这里获取的是Application
final context = await android_app_Application.get();
// 创建定位客户端
_androidClient ??= await com_amap_api_location_AMapLocationClient
.create__android_content_Context(context);
},
ios: (pool) async {
await AmapCore.init(iosKey);
_iosClient ??= await AMapLocationManager.create__();
},
);
}
/// 隐私是否已经展示
Future<void> updatePrivacyShow(bool value) async {
return platform(
android: (pool) async {
final context = await android_app_Application.get();
await com_amap_api_location_AMapLocationClient.updatePrivacyShow(
context, value, true);
},
ios: (pool) async {
AMapLocationManager.updatePrivacyShow_privacyInfo(
value
? AMapPrivacyShowStatus.AMapPrivacyShowStatusDidShow
: AMapPrivacyShowStatus.AMapPrivacyShowStatusNotShow,
AMapPrivacyInfoStatus.AMapPrivacyInfoStatusDidContain,
);
},
);
}
/// 隐私是否已经同意
Future<void> updatePrivacyAgree(bool value) async {
return platform(
android: (pool) async {
final context = await android_app_Application.get();
await com_amap_api_location_AMapLocationClient.updatePrivacyAgree(
context,
value,
);
},
ios: (pool) async {
await AMapLocationManager.updatePrivacyAgree(
value
? AMapPrivacyAgreeStatus.AMapPrivacyAgreeStatusDidAgree
: AMapPrivacyAgreeStatus.AMapPrivacyAgreeStatusNotAgree,
);
},
);
}
/// 单次获取定位信息
///
/// 选择定位模式[mode], 设置定位同时是否需要返回地址描述[needAddress], 设置定位请求超时时间,默认为30秒[timeout].
Future<Location> fetchLocation({
LocationAccuracy mode = LocationAccuracy.Low,
bool? needAddress,
Duration? timeout,
}) async {
var completer = Completer<Location>();
return platform(
android: (pool) async {
assert(_androidClient != null,
'请先在main方法中调用AmapLocation.instance.init()进行初始化!');
final listener =
await com_amap_api_location_AMapLocationListener.anonymous__();
listener.onLocationChanged = (location) async {
if (!completer.isCompleted) {
completer.complete(
Location(
address: await location!.getAddress(),
latLng: LatLng(
await location.getLatitude() ?? 0,
await location.getLongitude() ?? 0,
),
altitude: await location.getAltitude(),
bearing: await location.getBearing(),
country: await location.getCountry(),
province: await location.getProvince(),
city: await location.getCity(),
cityCode: await location.getCityCode(),
adCode: await location.getAdCode(),
district: await location.getDistrict(),
poiName: await location.getPoiName(),
street: await location.getStreet(),
streetNumber: await location.getStreetNum(),
aoiName: await location.getAoiName(),
accuracy: await location.getAccuracy(),
speed: await location.speed,
),
);
}
};
await _androidClient?.setLocationListener(listener);
// 创建选项
final options =
await com_amap_api_location_AMapLocationClientOption.create__();
// 设置单次定位
await options.setOnceLocation(true);
// 设置定位模式
switch (mode) {
// 高精度定位模式:会同时使用网络定位和GPS定位,优先返回最高精度的定位结果,以及对应的地址描述信息。
case LocationAccuracy.High:
await options.setLocationMode(
com_amap_api_location_AMapLocationClientOption_AMapLocationMode
.Hight_Accuracy);
break;
// 低功耗定位模式:不会使用GPS和其他传感器,只会使用网络定位(Wi-Fi和基站定位);
case LocationAccuracy.Low:
await options.setLocationMode(
com_amap_api_location_AMapLocationClientOption_AMapLocationMode
.Battery_Saving);
break;
case LocationAccuracy.DeviceSensor:
await options.setLocationMode(
com_amap_api_location_AMapLocationClientOption_AMapLocationMode
.Device_Sensors);
break;
}
// 是否返回地址描述
if (needAddress != null) await options.setNeedAddress(needAddress);
// 设置定位请求超时时间,默认为30秒。
if (timeout != null) {
await options.setHttpTimeOut(timeout.inMilliseconds);
}
await options.setSensorEnable(true);
// 设置选项
await _androidClient?.setLocationOption(options);
// 开始定位
await _androidClient?.startLocation();
return completer.future;
},
ios: (pool) async {
assert(_iosClient != null,
'请先在main方法中调用AmapLocation.instance.init()进行初始化!');
// 设置定位模式
switch (mode) {
// 高精度定位模式:会同时使用网络定位和GPS定位,优先返回最高精度的定位结果,以及对应的地址描述信息。
case LocationAccuracy.High:
await _iosClient?.set_desiredAccuracy(10);
break;
// 低功耗定位模式:不会使用GPS和其他传感器,只会使用网络定位(Wi-Fi和基站定位);
case LocationAccuracy.DeviceSensor:
case LocationAccuracy.Low:
await _iosClient?.set_desiredAccuracy(100);
break;
}
// 设置定位请求超时时间,默认为30秒。
if (timeout != null) {
await _iosClient?.set_locationTimeout(timeout.inSeconds);
}
// 设置回调
_iosLocationDelegate ??= await AMapLocationManagerDelegate.anonymous__();
_iosLocationDelegate!.amapLocationManager_doRequireTemporaryFullAccuracyAuth_completion =
(AMapLocationManager? manager, CLLocationManager? locationManager, void Function(NSError? error) completion) async {
print("----- 单次获取定位信息 doRequireTemporaryFullAccuracyAuth ----");
locationManager?.requestAlwaysAuthorization();
locationManager?.requestWhenInUseAuthorization();
};
_iosLocationDelegate!.amapLocationManager_doRequireLocationAuth =
(AMapLocationManager? manager, CLLocationManager? locationManager) async {
print("----- 单次获取定位信息 doRequireLocationAuth ----");
locationManager?.requestAlwaysAuthorization();
locationManager?.requestWhenInUseAuthorization();
};
await _iosClient?.set_delegate(_iosLocationDelegate!);
await _iosClient?.set_locatingWithReGeocode(true);
await _iosClient?.startUpdatingLocation();
// 单次获取定位
await _iosClient?.requestLocationWithReGeocode_completionBlock(
needAddress ?? true,
(location, regeocode, error) async {
if (!completer.isCompleted) {
final latitude =
await location!.coordinate.then((it) => it.latitude);
final longitude =
await location.coordinate.then((it) => it.longitude);
completer.complete(Location(
address: await regeocode?.get_formattedAddress(),
latLng: LatLng(await latitude ?? 0, await longitude ?? 0),
altitude: await location.altitude,
bearing: await location.course,
country: await regeocode?.get_country(),
province: await regeocode?.get_province(),
city: await regeocode?.get_city(),
cityCode: await regeocode?.get_citycode(),
adCode: await regeocode?.get_adcode(),
district: await regeocode?.get_district(),
poiName: await regeocode?.get_POIName(),
street: await regeocode?.get_street(),
streetNumber: await regeocode?.get_number(),
aoiName: await regeocode?.get_AOIName(),
accuracy: await location.horizontalAccuracy,
speed: await location.speed,
));
}
},
);
return completer.future;
},
);
}
/// 连续获取定位信息
///
/// 选择定位模式[mode], 设置定位同时是否需要返回地址描述[needAddress], 设置定位请求超时时间,默认为30秒[timeout]
/// 设置定位间隔[interval], 默认2000 ms, 设置是否开启定位缓存机制[cacheEnable].
/// [distanceFilter] ios only: 设置更新定位的最小偏移距离, 单位:米.
Stream<Location> listenLocation({
LocationAccuracy mode = LocationAccuracy.Low,
bool? needAddress,
Duration? timeout,
int? interval,
double? distanceFilter,
}) async* {
_locationController ??= StreamController<Location>();
if (Platform.isAndroid) {
final listener =
await com_amap_api_location_AMapLocationListener.anonymous__();
listener.onLocationChanged = (location) async {
if (!_locationController!.isClosed) {
_locationController!.add(
Location(
address: await location!.getAddress(),
latLng: LatLng(
await location.getLatitude() ?? 0,
await location.getLongitude() ?? 0,
),
altitude: await location.getAltitude(),
bearing: await location.getBearing(),
country: await location.getCountry(),
province: await location.getProvince(),
city: await location.getCity(),
cityCode: await location.getCityCode(),
adCode: await location.getAdCode(),
district: await location.getDistrict(),
poiName: await location.getPoiName(),
street: await location.getStreet(),
streetNumber: await location.getStreetNum(),
aoiName: await location.getAoiName(),
accuracy: await location.getAccuracy(),
speed: await location.speed,
),
);
}
};
await _androidClient?.setLocationListener(listener);
// 创建选项
final options =
await com_amap_api_location_AMapLocationClientOption.create__();
// 设置连续定位
await options.setOnceLocation(false);
// 设置定位模式
switch (mode) {
// 高精度定位模式:会同时使用网络定位和GPS定位,优先返回最高精度的定位结果,以及对应的地址描述信息。
case LocationAccuracy.High:
await options.setLocationMode(
com_amap_api_location_AMapLocationClientOption_AMapLocationMode
.Hight_Accuracy);
break;
// 低功耗定位模式:不会使用GPS和其他传感器,只会使用网络定位(Wi-Fi和基站定位);
case LocationAccuracy.Low:
await options.setLocationMode(
com_amap_api_location_AMapLocationClientOption_AMapLocationMode
.Battery_Saving);
break;
case LocationAccuracy.DeviceSensor:
await options.setLocationMode(
com_amap_api_location_AMapLocationClientOption_AMapLocationMode
.Device_Sensors);
break;
}
// 是否返回地址描述
if (needAddress != null) await options.setNeedAddress(needAddress);
// 设置定位请求超时时间,默认为30秒。
if (timeout != null) await options.setHttpTimeOut(timeout.inSeconds);
// 设置定位间隔
if (interval != null) await options.setInterval(interval);
await options.setSensorEnable(true);
// 设置选项
await _androidClient?.setLocationOption(options);
// 开始定位
await _androidClient?.startLocation();
yield* _locationController!.stream;
} else if (Platform.isIOS) {
// 设置定位模式
switch (mode) {
// 高精度定位模式:会同时使用网络定位和GPS定位,优先返回最高精度的定位结果,以及对应的地址描述信息。
case LocationAccuracy.High:
await _iosClient?.set_desiredAccuracy(10);
break;
// 低功耗定位模式:不会使用GPS和其他传感器,只会使用网络定位(Wi-Fi和基站定位);
case LocationAccuracy.Low:
case LocationAccuracy.DeviceSensor:
await _iosClient?.set_desiredAccuracy(100);
break;
}
// 设置定位请求超时时间,默认为30秒。
if (timeout != null) {
await _iosClient?.set_locationTimeout(timeout.inSeconds);
}
// 设定定位的最小更新距离
if (distanceFilter != null) {
await _iosClient?.set_distanceFilter(distanceFilter);
}
// 设置回调
_iosLocationDelegate ??= await AMapLocationManagerDelegate.anonymous__();
_iosLocationDelegate!.amapLocationManager_didUpdateLocation_reGeocode =
(_, location, regeocode) async {
final latitude = await location!.coordinate.then((it) => it.latitude);
final longitude = await location.coordinate.then((it) => it.longitude);
if (!_locationController!.isClosed) {
_locationController!.add(Location(
address: await regeocode?.get_formattedAddress(),
latLng: LatLng(await latitude ?? 0, await longitude ?? 0),
altitude: await location.altitude,
bearing: await location.course,
country: await regeocode?.get_country(),
province: await regeocode?.get_province(),
city: await regeocode?.get_city(),
cityCode: await regeocode?.get_citycode(),
adCode: await regeocode?.get_adcode(),
district: await regeocode?.get_district(),
poiName: await regeocode?.get_POIName(),
street: await regeocode?.get_street(),
streetNumber: await regeocode?.get_number(),
aoiName: await regeocode?.get_AOIName(),
accuracy: await location.horizontalAccuracy,
speed: await location.speed,
));
}
};
await _iosClient?.set_delegate(_iosLocationDelegate!);
await _iosClient?.set_locatingWithReGeocode(true);
await _iosClient?.startUpdatingLocation();
yield* _locationController!.stream;
}
}
/// 停止定位
Future<void> stopLocation() {
return platform(
android: (pool) async {
await _locationController?.close();
_locationController = null;
await _androidClient?.stopLocation();
},
ios: (pool) async {
await _locationController?.close();
_locationController = null;
await _iosClient?.stopUpdatingLocation();
},
);
}
/// 开启后台定位
Future<void> enableBackgroundLocation(
int id,
BackgroundNotification bgNotification,
) {
return platform(
android: (pool) async {
final notification = await android_app_Notification.create(
contentTitle: bgNotification.contentTitle,
contentText: bgNotification.contentText,
when: bgNotification.when,
channelId: bgNotification.channelId,
channelName: bgNotification.channelName,
enableLights: bgNotification.enableLights,
showBadge: bgNotification.showBadge,
);
await checkClient();
await _androidClient?.enableBackgroundLocation(id, notification);
pool..add(notification);
},
ios: (pool) async {
await _iosClient?.set_allowsBackgroundLocationUpdates(true);
await _iosClient?.set_pausesLocationUpdatesAutomatically(false);
},
);
}
/// 关闭后台定位
Future<void> disableBackgroundLocation(bool var1) {
return platform(
android: (pool) async {
await checkClient();
await _androidClient?.disableBackgroundLocation(var1);
},
ios: (pool) async {
await _iosClient?.set_allowsBackgroundLocationUpdates(false);
await _iosClient?.set_pausesLocationUpdatesAutomatically(true);
},
);
}
/// 确保client不为空
Future<void> checkClient() async {
if (Platform.isAndroid) {
// 获取上下文, 这里获取的是Application
final context = await android_app_Application.get();
// 创建定位客户端
_androidClient ??= await com_amap_api_location_AMapLocationClient
.create__android_content_Context(context);
} else if (Platform.isIOS) {
_iosClient ??= await AMapLocationManager.create__();
}
}
void initAndroidListener() {
if (Platform.isAndroid) {
// 电子围栏回调
MethodChannel(
'com.amap.api.fence.GeoFenceClient::addGeoFenceX::Callback',
kAmapLocationFluttifyMethodCodec,
).setMethodCallHandler((call) async {
if (call.method ==
'Callback::com.amap.api.fence.GeoFenceClient::addGeoFenceX') {
final args = await call.arguments as Map;
final status = args['status'] as int;
final customId = args['customId'] as String;
final fenceId = args['fenceId'] as String;
debugPrint(
'收到围栏消息: status: $status, customId: $customId, fenceId:$fenceId');
final fence = com_amap_api_fence_GeoFence()
..refId = (args['fence'] as Ref).refId;
_geoFenceEventController?.add(
GeoFenceEvent(
customId: customId,
fenceId: fenceId,
status: GeoFenceStatusX.fromAndroid(status),
genFence: GeoFence.android(fence),
),
);
}
});
}
}
/// 创建圆形电子围栏
Stream<GeoFenceEvent> addCircleGeoFence({
required LatLng center,
required double radius,
String customId = '',
List<GeoFenceActiveAction> activeActions = const [
GeoFenceActiveAction.In,
GeoFenceActiveAction.Out,
GeoFenceActiveAction.Stayed,
],
}) async* {
_geoFenceEventController ??= StreamController<GeoFenceEvent>.broadcast();
if (Platform.isAndroid) {
final context = await android_app_Application.get();
_androidGeoFenceClient ??= await com_amap_api_fence_GeoFenceClient
.create__android_content_Context(context);
final point = await com_amap_api_location_DPoint.create__double__double(
center.latitude, center.longitude);
await _androidGeoFenceClient?.addCircleGeoFence(
activeActions.getActiveAction(),
point,
radius,
customId,
);
} else if (Platform.isIOS) {
_iosGeoFenceClient ??= await AMapGeoFenceManager.create__();
_iosGeoFenceDelegate ??= await AMapGeoFenceManagerDelegate.anonymous__();
_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),
),
);
};
_iosGeoFenceClient!.set_delegate(_iosGeoFenceDelegate!);
await _iosGeoFenceClient
?.set_activeActionX(activeActions.getActiveAction());
await _iosGeoFenceClient?.set_allowsBackgroundLocationUpdates(true);
final point = await CLLocationCoordinate2D.create(
center.latitude,
center.longitude,
);
await _iosGeoFenceClient
?.addCircleRegionForMonitoringWithCenter_radius_customID(
point, radius, customId);
} else {
throw '未实现的平台';
}
yield* _geoFenceEventController!.stream;
}
/// 创POI电子围栏
Stream<GeoFenceEvent> addPoiGeoFence({
required String keyword,
String poiType = '',
String city = '',
int aroundRadius = 10,
String customId = '',
List<GeoFenceActiveAction> activeActions = const [
GeoFenceActiveAction.In,
GeoFenceActiveAction.Out,
GeoFenceActiveAction.Stayed,
],
}) async* {
_geoFenceEventController ??= StreamController<GeoFenceEvent>.broadcast();
if (Platform.isAndroid) {
final context = await android_app_Application.get();
_androidGeoFenceClient ??= await com_amap_api_fence_GeoFenceClient
.create__android_content_Context(context);
await _androidGeoFenceClient?.addPoiGeoFence(
keyword: keyword,
poiType: poiType,
city: city,
aroundRadius: aroundRadius,
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);
await _iosGeoFenceClient
?.addKeywordPOIRegionForMonitoringWithKeyword_POIType_city_size_customID(
keyword,
poiType,
city,
aroundRadius,
customId,
);
} else {
throw '未实现的平台';
}
yield* _geoFenceEventController!.stream;
}
/// 创建多边形电子围栏
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;
}
/// 创建行政区划电子围栏
Stream<GeoFenceEvent> addDistrictGeoFence({
required String keyword,
String customId = '',
List<GeoFenceActiveAction> activeActions = const [
GeoFenceActiveAction.In,
GeoFenceActiveAction.Out,
GeoFenceActiveAction.Stayed,
],
}) async* {
_geoFenceEventController ??= StreamController<GeoFenceEvent>.broadcast();
if (Platform.isAndroid) {
final context = await android_app_Application.get();
_androidGeoFenceClient ??= await com_amap_api_fence_GeoFenceClient
.create__android_content_Context(context);
await _androidGeoFenceClient?.addDistrictGeoFence(
keyword: keyword,
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);
await _iosGeoFenceClient
?.addDistrictRegionForMonitoringWithDistrictName_customID(
keyword, customId);
} else {
throw '未实现的平台';
}
yield* _geoFenceEventController!.stream;
}
/// 删除单个围栏
Future<void> removeGeoFence(GeoFence geoFence) async {
return platform(
android: (pool) async {
await _androidGeoFenceClient
?.removeGeoFence__com_amap_api_fence_GeoFence(
geoFence.androidModel!);
},
ios: (pool) async {
await _iosGeoFenceClient?.removeTheGeoFenceRegion(geoFence.iosModel!);
},
);
}
/// 删除所有围栏
Future<void> removeAllGeoFence() async {
return platform(
android: (pool) async {
await _androidGeoFenceClient?.removeGeoFence();
},
ios: (pool) async {
await _iosGeoFenceClient?.removeAllGeoFenceRegions();
},
);
}
/// 释放对象, 如果[AmapLocationDisposeMixin]不能满足需求时再使用这个方法
Future<void> dispose() async {
await _locationController?.close();
_locationController = null;
await _geoFenceEventController?.close();
_geoFenceEventController = null;
// 取消注册广播
if (Platform.isAndroid) {
await kAmapLocationFluttifyChannel.invokeMethod(
'com.amap.api.fence.GeoFenceClient::unregisterBroadcastReceiver');
}
await _androidClient?.onDestroy();
await _androidClient?.release__();
await _iosClient?.release__();
final isCurrentPlugin = (Ref it) => it.tag__ == 'amap_location_fluttify';
await gGlobalReleasePool.where(isCurrentPlugin).release_batch();
gGlobalReleasePool.removeWhere(isCurrentPlugin);
_androidClient = null;
_iosClient = null;
}
}