|
|
|
@ -2,11 +2,64 @@ import 'dart:io';
|
|
|
|
|
|
|
|
|
|
import 'package:android_intent_plus/android_intent.dart'; |
|
|
|
|
import 'package:flutter/cupertino.dart'; |
|
|
|
|
import 'package:flutter_bmflocation/bdmap_location_flutter_plugin.dart'; |
|
|
|
|
import 'package:flutter_bmflocation/flutter_bmflocation.dart'; |
|
|
|
|
import 'package:huixiang/generated/l10n.dart'; |
|
|
|
|
import 'package:huixiang/view_widget/request_permission.dart'; |
|
|
|
|
import 'package:permission_handler/permission_handler.dart'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BaiduLocationAndroidOption initAndroidOptions() { |
|
|
|
|
BaiduLocationAndroidOption options = BaiduLocationAndroidOption( |
|
|
|
|
// 定位模式,可选的模式有高精度、仅设备、仅网络。默认为高精度模式 |
|
|
|
|
locationMode: BMFLocationMode.hightAccuracy, |
|
|
|
|
// 是否需要返回地址信息 |
|
|
|
|
isNeedAddress: true, |
|
|
|
|
// 是否需要返回海拔高度信息 |
|
|
|
|
isNeedAltitude: false, |
|
|
|
|
// 是否需要返回周边poi信息 |
|
|
|
|
isNeedLocationPoiList: false, |
|
|
|
|
// 是否需要返回新版本rgc信息 |
|
|
|
|
isNeedNewVersionRgc: false, |
|
|
|
|
// 是否需要返回位置描述信息 |
|
|
|
|
isNeedLocationDescribe: true, |
|
|
|
|
// 是否使用gps |
|
|
|
|
openGps: true, |
|
|
|
|
// 可选,设置场景定位参数,包括签到场景、运动场景、出行场景 |
|
|
|
|
locationPurpose: BMFLocationPurpose.signIn, |
|
|
|
|
// 坐标系 |
|
|
|
|
coordType: BMFLocationCoordType.bd09ll, |
|
|
|
|
// 设置发起定位请求的间隔,int类型,单位ms |
|
|
|
|
// 如果设置为0,则代表单次定位,即仅定位一次,默认为0 |
|
|
|
|
scanspan: 0); |
|
|
|
|
return options; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
BaiduLocationIOSOption initIOSOptions() { |
|
|
|
|
BaiduLocationIOSOption options = BaiduLocationIOSOption( |
|
|
|
|
// 坐标系 |
|
|
|
|
coordType: BMFLocationCoordType.bd09ll, |
|
|
|
|
// 位置获取超时时间 |
|
|
|
|
locationTimeout: 10, |
|
|
|
|
// 获取地址信息超时时间 |
|
|
|
|
reGeocodeTimeout: 10, |
|
|
|
|
// 应用位置类型 默认为automotiveNavigation |
|
|
|
|
activityType: BMFActivityType.automotiveNavigation, |
|
|
|
|
// 设置预期精度参数 默认为best |
|
|
|
|
desiredAccuracy: BMFDesiredAccuracy.best, |
|
|
|
|
// 是否需要最新版本rgc数据 |
|
|
|
|
isNeedNewVersionRgc: false, |
|
|
|
|
// 指定定位是否会被系统自动暂停 |
|
|
|
|
pausesLocationUpdatesAutomatically: false, |
|
|
|
|
// 指定是否允许后台定位, |
|
|
|
|
// 允许的话是可以进行后台定位的,但需要项目配置允许后台定位,否则会报错,具体参考开发文档 |
|
|
|
|
allowsBackgroundLocationUpdates: false, |
|
|
|
|
// 设定定位的最小更新距离 |
|
|
|
|
distanceFilter: 10, |
|
|
|
|
); |
|
|
|
|
return options; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Location { |
|
|
|
|
static Location _instance; |
|
|
|
|
|
|
|
|
@ -23,37 +76,27 @@ class Location {
|
|
|
|
|
|
|
|
|
|
LocationFlutterPlugin aMapFlutterLocation; |
|
|
|
|
|
|
|
|
|
prepareLoc() { |
|
|
|
|
aMapFlutterLocation.prepareLoc({ |
|
|
|
|
"coorType": "bd09ll", |
|
|
|
|
"isNeedAddres": false, |
|
|
|
|
"isNeedAltitude": false, |
|
|
|
|
"isNeedLocationPoiList": false, |
|
|
|
|
"isNeedLocationDescribe": false, |
|
|
|
|
"isNeedNewVersionRgc": false, |
|
|
|
|
"scanspan": 0, |
|
|
|
|
"openGps": true, |
|
|
|
|
"locationMode": 2, |
|
|
|
|
}, { |
|
|
|
|
"locationMode": "kCLLocationAccuracyBest", |
|
|
|
|
"locationTimeout": 10, |
|
|
|
|
"reGeocodeTimeout": 10, |
|
|
|
|
"activityType": "CLActivityTypeAutomotiveNavigation", |
|
|
|
|
"BMKLocationCoordinateType": "BMKLocationCoordinateTypeBMK09LL", |
|
|
|
|
"isNeedNewVersionRgc": false, |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Future<bool> startLocation(context) async { |
|
|
|
|
Future<bool> startLocation(context, Function(BaiduLocation result) locationCallback) async { |
|
|
|
|
aMapFlutterLocation.prepareLoc(initAndroidOptions().getMap(), initIOSOptions().getMap()); |
|
|
|
|
if (!(await Permission.locationWhenInUse.serviceStatus.isEnabled)) { |
|
|
|
|
enableLocation(context); |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
if (await Permission.location.isPermanentlyDenied) { |
|
|
|
|
requestDialog(context); |
|
|
|
|
requestDialog(context, locationCallback); |
|
|
|
|
return false; |
|
|
|
|
} else if (await Permission.location.isGranted) { |
|
|
|
|
aMapFlutterLocation.startLocation(); |
|
|
|
|
aMapFlutterLocation.singleLocationCallback(callback: (BaiduLocation result) { |
|
|
|
|
locationCallback.call(result); |
|
|
|
|
}); |
|
|
|
|
if (Platform.isIOS) { |
|
|
|
|
await aMapFlutterLocation.singleLocation({ |
|
|
|
|
'isReGeocode': true, |
|
|
|
|
'isNetworkState': true, |
|
|
|
|
}); |
|
|
|
|
} else if (Platform.isAndroid) { |
|
|
|
|
await aMapFlutterLocation.startLocation(); |
|
|
|
|
} |
|
|
|
|
return true; |
|
|
|
|
} else if (await Permission.location.isUndetermined) { |
|
|
|
|
await Permission.location.request(); |
|
|
|
@ -61,7 +104,7 @@ class Location {
|
|
|
|
|
} else { |
|
|
|
|
if (Platform.isIOS) { |
|
|
|
|
//去设置中心 |
|
|
|
|
requestDialog(context); |
|
|
|
|
requestDialog(context, locationCallback); |
|
|
|
|
} else { |
|
|
|
|
await Permission.location.request(); |
|
|
|
|
} |
|
|
|
@ -97,7 +140,7 @@ class Location {
|
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
requestDialog(context) { |
|
|
|
|
requestDialog(context, Function(BaiduLocation result) locationCallback) { |
|
|
|
|
showCupertinoDialog( |
|
|
|
|
context: context, |
|
|
|
|
builder: (context) { |
|
|
|
@ -110,7 +153,7 @@ class Location {
|
|
|
|
|
if (result) { |
|
|
|
|
await openAppSettings(); |
|
|
|
|
if (await Permission.location.isGranted) { |
|
|
|
|
startLocation(context); |
|
|
|
|
startLocation(context, locationCallback); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|