123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- library amap_location;
- import 'dart:async';
- import 'package:flutter/foundation.dart';
- import 'package:flutter/services.dart';
- /// 仅Android可用
- enum AmapLocationMode {
- /// 高精度模式
- HIGHT_ACCURACY,
- /// 低功耗模式
- BATTERY_SAVING,
- /// 仅设备模式,不支持室内环境的定位
- DEVICE_SENSORS,
- }
- /// 仅IOS可用
- enum AmapLocationAccuracy {
- /// 最快 精确度最底 约秒到
- THREE_KILOMETERS,
- /// 精确度较低 约秒到
- KILOMETER,
- /// 精确度较低 约2s
- HUNDREE_METERS,
- /// 精确度较高 约5s
- NEAREST_TENMETERS,
- /// 最慢 精确度最高 约8s
- BEST,
- }
- enum ConvertType {
- /// GPS
- GPS,
- /// 百度
- BAIDU,
- /// Google
- GOOGLE,
- }
- typedef void AmapLocationListen(Location location);
- class AmapLocation {
- static const _channel = MethodChannel('plugins.ouj.com/amap_location');
- static const _event = EventChannel('plugins.ouj.com/amap_location_event');
- static StreamController<dynamic> customStreamController = StreamController<dynamic>();
- static Stream<dynamic> get _eventStream {
- if (kIsWeb) {
- return customStreamController.stream;
- } else {
- return _event.receiveBroadcastStream();
- }
- }
- /// 单次定位
- ///
- /// androidMode 定位方式 [ 仅适用android ]
- ///
- /// iosAccuracy 精确度 [ 仅适用ios ]
- static Future<Location> fetch({
- AmapLocationMode androidMode = AmapLocationMode.HIGHT_ACCURACY,
- AmapLocationAccuracy iosAccuracy = AmapLocationAccuracy.THREE_KILOMETERS,
- bool gps = false,
- int gpsTime = 20000,
- }) async {
- dynamic location = await _channel.invokeMethod('fetch', {
- 'mode': androidMode.index,
- 'accuracy': iosAccuracy.index,
- 'gps' : gps,
- 'gpsTime' : gpsTime
- });
- return Location.fromJson(location);
- }
- /// 持续定位
- ///
- /// time 间隔时间 默认 2000
- ///
- /// mode 定位方式 [ 仅适用android ]
- ///
- /// geocode 返回逆编码信息
- ///
- /// accuracy 精确度 [ 仅适用ios ]
- static Future<Future<Null> Function()> start({
- AmapLocationListen? listen,
- AmapLocationMode mode = AmapLocationMode.HIGHT_ACCURACY,
- int? time,
- bool? needAddress = false,
- AmapLocationAccuracy accuracy = AmapLocationAccuracy.THREE_KILOMETERS,
- }) async {
- await _channel.invokeMethod('start', {
- 'mode': mode.index,
- 'time': time ?? 2000,
- 'accuracy': accuracy.index,
- });
- StreamSubscription<dynamic>? _stream = _eventStream.listen((dynamic data) {
- listen!(Location.fromJson(data));
- });
- return () async {
- await _channel.invokeMethod('stop');
- _stream.cancel();
- };
- }
- /// 启动后台服务
- static Future<void> enableBackground({
- required String title,
- required String label,
- required String assetName,
- bool? vibrate,
- }) async {
- await _channel.invokeMethod('enableBackground', {'title': title, 'label': label, 'assetName': assetName, 'vibrate': vibrate ?? true});
- }
- /// 关闭后台服务
- static Future<void> disableBackground() async {
- await _channel.invokeMethod('disableBackground');
- }
- }
- class Location {
- double? latitude;
- double? longitude;
- double? altitude;
- final double? speed;
- /// 地址
- final String? address;
- /// 国家
- final String? country;
- /// 省
- final String? province;
- /// 市
- final String? city;
- final String? citycode;
- final String? adcode;
- /// 区
- final String? district;
- /// 街道
- final String? street;
- /// 精准度 [在web端直接返回0]
- final double? accuracy;
- final double? bearing;
- final int? locationType;
- int? state;
- int? time;
- int? step;
- Location({
- this.latitude,
- this.longitude,
- this.accuracy,
- this.altitude,
- this.speed,
- this.address,
- this.city,
- this.country,
- this.district,
- this.street,
- this.province,
- this.bearing,
- this.citycode,this.adcode,this.state,this.time,this.step,
- this.locationType
- });
- factory Location.fromJson(Map<dynamic, dynamic> json) => _$LocationFromJson(json);
- Map<String, dynamic> toJson() => _$LocationToJson(this);
- Location copyWith({
- double? latitude,
- double? longitude,
- double? altitude,
- double? speed,
- String? address,
- String? country,
- String? province,
- String? city,
- String? citycode,
- String? adcode,
- String? district,
- String? street,
- double? accuracy,
- int? state,
- int? time,
- int? step,
- double? bearing,
- int? locationType,
- }) {
- return new Location(
- latitude: latitude ?? this.latitude,
- longitude: longitude ?? this.longitude,
- altitude: altitude ?? this.altitude,
- speed: speed ?? this.speed,
- address: address ?? this.address,
- country: country ?? this.country,
- province: province ?? this.province,
- city: city ?? this.city,
- citycode: citycode ?? this.citycode,
- adcode: adcode ?? this.adcode,
- district: district ?? this.district,
- street: street ?? this.street,
- accuracy: accuracy ?? this.accuracy,
- state: state ?? this.state,
- time: time ?? this.time,
- step: step ?? this.step,
- bearing: bearing ?? this.bearing,
- locationType: locationType ?? this.locationType,
- );
- }
- }
- Location _$LocationFromJson(Map<dynamic, dynamic> json) {
- return Location(
- latitude: (json['latitude'] as num).toDouble(),
- longitude: (json['longitude'] as num).toDouble(),
- accuracy: (json['accuracy'] as num?)?.toDouble() ?? 0,
- altitude: (json['altitude'] as num?)?.toDouble(),
- address: json['address'] as String,
- city: json['city'] as String,
- country: json['country'] as String,
- district: json['district'] as String,
- street: json['street'] as String,
- province: json['province'] as String,
- citycode: json['citycode'] as String,
- adcode: json['adcode'] as String,
- speed: (json['speed'] as num?)?.toDouble() ?? 0,
- step: (json['step'] as num?)?.toInt() ?? 0,
- state: (json['state'] as num?)?.toInt() ?? 0,
- time: (json['time'] as num?)?.toInt() ?? 0,
- bearing: (json['bearing'] as num?)?.toDouble() ?? 0,
- locationType: (json['locationType'] as num?)?.toInt() ?? 0,
- );
- }
- Map<String, dynamic> _$LocationToJson(Location instance) => <String, dynamic>{
- 'latitude': instance.latitude,
- 'longitude': instance.longitude,
- 'accuracy': instance.accuracy,
- 'bearing': instance.bearing,
- 'locationType': instance.locationType,
- 'speed': instance.speed,
- 'altitude': instance.altitude,
- };
|