amap_location.dart 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. library amap_location;
  2. import 'dart:async';
  3. import 'package:flutter/foundation.dart';
  4. import 'package:flutter/services.dart';
  5. /// 仅Android可用
  6. enum AmapLocationMode {
  7. /// 高精度模式
  8. HIGHT_ACCURACY,
  9. /// 低功耗模式
  10. BATTERY_SAVING,
  11. /// 仅设备模式,不支持室内环境的定位
  12. DEVICE_SENSORS,
  13. }
  14. /// 仅IOS可用
  15. enum AmapLocationAccuracy {
  16. /// 最快 精确度最底 约秒到
  17. THREE_KILOMETERS,
  18. /// 精确度较低 约秒到
  19. KILOMETER,
  20. /// 精确度较低 约2s
  21. HUNDREE_METERS,
  22. /// 精确度较高 约5s
  23. NEAREST_TENMETERS,
  24. /// 最慢 精确度最高 约8s
  25. BEST,
  26. }
  27. enum ConvertType {
  28. /// GPS
  29. GPS,
  30. /// 百度
  31. BAIDU,
  32. /// Google
  33. GOOGLE,
  34. }
  35. typedef void AmapLocationListen(Location location);
  36. class AmapLocation {
  37. static const _channel = MethodChannel('plugins.ouj.com/amap_location');
  38. static const _event = EventChannel('plugins.ouj.com/amap_location_event');
  39. static StreamController<dynamic> customStreamController = StreamController<dynamic>();
  40. static Stream<dynamic> get _eventStream {
  41. if (kIsWeb) {
  42. return customStreamController.stream;
  43. } else {
  44. return _event.receiveBroadcastStream();
  45. }
  46. }
  47. /// 单次定位
  48. ///
  49. /// androidMode 定位方式 [ 仅适用android ]
  50. ///
  51. /// iosAccuracy 精确度 [ 仅适用ios ]
  52. static Future<Location> fetch({
  53. AmapLocationMode androidMode = AmapLocationMode.HIGHT_ACCURACY,
  54. AmapLocationAccuracy iosAccuracy = AmapLocationAccuracy.THREE_KILOMETERS,
  55. bool gps = false,
  56. int gpsTime = 20000,
  57. }) async {
  58. dynamic location = await _channel.invokeMethod('fetch', {
  59. 'mode': androidMode.index,
  60. 'accuracy': iosAccuracy.index,
  61. 'gps' : gps,
  62. 'gpsTime' : gpsTime
  63. });
  64. return Location.fromJson(location);
  65. }
  66. /// 持续定位
  67. ///
  68. /// time 间隔时间 默认 2000
  69. ///
  70. /// mode 定位方式 [ 仅适用android ]
  71. ///
  72. /// geocode 返回逆编码信息
  73. ///
  74. /// accuracy 精确度 [ 仅适用ios ]
  75. static Future<Future<Null> Function()> start({
  76. AmapLocationListen? listen,
  77. AmapLocationMode mode = AmapLocationMode.HIGHT_ACCURACY,
  78. int? time,
  79. bool? needAddress = false,
  80. AmapLocationAccuracy accuracy = AmapLocationAccuracy.THREE_KILOMETERS,
  81. }) async {
  82. await _channel.invokeMethod('start', {
  83. 'mode': mode.index,
  84. 'time': time ?? 2000,
  85. 'accuracy': accuracy.index,
  86. });
  87. StreamSubscription<dynamic>? _stream = _eventStream.listen((dynamic data) {
  88. listen!(Location.fromJson(data));
  89. });
  90. return () async {
  91. await _channel.invokeMethod('stop');
  92. _stream.cancel();
  93. };
  94. }
  95. /// 启动后台服务
  96. static Future<void> enableBackground({
  97. required String title,
  98. required String label,
  99. required String assetName,
  100. bool? vibrate,
  101. }) async {
  102. await _channel.invokeMethod('enableBackground', {'title': title, 'label': label, 'assetName': assetName, 'vibrate': vibrate ?? true});
  103. }
  104. /// 关闭后台服务
  105. static Future<void> disableBackground() async {
  106. await _channel.invokeMethod('disableBackground');
  107. }
  108. }
  109. class Location {
  110. double? latitude;
  111. double? longitude;
  112. double? altitude;
  113. final double? speed;
  114. /// 地址
  115. final String? address;
  116. /// 国家
  117. final String? country;
  118. /// 省
  119. final String? province;
  120. /// 市
  121. final String? city;
  122. final String? citycode;
  123. final String? adcode;
  124. /// 区
  125. final String? district;
  126. /// 街道
  127. final String? street;
  128. /// 精准度 [在web端直接返回0]
  129. final double? accuracy;
  130. final double? bearing;
  131. final int? locationType;
  132. int? state;
  133. int? time;
  134. int? step;
  135. Location({
  136. this.latitude,
  137. this.longitude,
  138. this.accuracy,
  139. this.altitude,
  140. this.speed,
  141. this.address,
  142. this.city,
  143. this.country,
  144. this.district,
  145. this.street,
  146. this.province,
  147. this.bearing,
  148. this.citycode,this.adcode,this.state,this.time,this.step,
  149. this.locationType
  150. });
  151. factory Location.fromJson(Map<dynamic, dynamic> json) => _$LocationFromJson(json);
  152. Map<String, dynamic> toJson() => _$LocationToJson(this);
  153. Location copyWith({
  154. double? latitude,
  155. double? longitude,
  156. double? altitude,
  157. double? speed,
  158. String? address,
  159. String? country,
  160. String? province,
  161. String? city,
  162. String? citycode,
  163. String? adcode,
  164. String? district,
  165. String? street,
  166. double? accuracy,
  167. int? state,
  168. int? time,
  169. int? step,
  170. double? bearing,
  171. int? locationType,
  172. }) {
  173. return new Location(
  174. latitude: latitude ?? this.latitude,
  175. longitude: longitude ?? this.longitude,
  176. altitude: altitude ?? this.altitude,
  177. speed: speed ?? this.speed,
  178. address: address ?? this.address,
  179. country: country ?? this.country,
  180. province: province ?? this.province,
  181. city: city ?? this.city,
  182. citycode: citycode ?? this.citycode,
  183. adcode: adcode ?? this.adcode,
  184. district: district ?? this.district,
  185. street: street ?? this.street,
  186. accuracy: accuracy ?? this.accuracy,
  187. state: state ?? this.state,
  188. time: time ?? this.time,
  189. step: step ?? this.step,
  190. bearing: bearing ?? this.bearing,
  191. locationType: locationType ?? this.locationType,
  192. );
  193. }
  194. }
  195. Location _$LocationFromJson(Map<dynamic, dynamic> json) {
  196. return Location(
  197. latitude: (json['latitude'] as num).toDouble(),
  198. longitude: (json['longitude'] as num).toDouble(),
  199. accuracy: (json['accuracy'] as num?)?.toDouble() ?? 0,
  200. altitude: (json['altitude'] as num?)?.toDouble(),
  201. address: json['address'] as String,
  202. city: json['city'] as String,
  203. country: json['country'] as String,
  204. district: json['district'] as String,
  205. street: json['street'] as String,
  206. province: json['province'] as String,
  207. citycode: json['citycode'] as String,
  208. adcode: json['adcode'] as String,
  209. speed: (json['speed'] as num?)?.toDouble() ?? 0,
  210. step: (json['step'] as num?)?.toInt() ?? 0,
  211. state: (json['state'] as num?)?.toInt() ?? 0,
  212. time: (json['time'] as num?)?.toInt() ?? 0,
  213. bearing: (json['bearing'] as num?)?.toDouble() ?? 0,
  214. locationType: (json['locationType'] as num?)?.toInt() ?? 0,
  215. );
  216. }
  217. Map<String, dynamic> _$LocationToJson(Location instance) => <String, dynamic>{
  218. 'latitude': instance.latitude,
  219. 'longitude': instance.longitude,
  220. 'accuracy': instance.accuracy,
  221. 'bearing': instance.bearing,
  222. 'locationType': instance.locationType,
  223. 'speed': instance.speed,
  224. 'altitude': instance.altitude,
  225. };