You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
5.4 KiB

3 years ago
import 'dart:io';
import 'package:android_intent_plus/android_intent.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_bmflocation/flutter_bmflocation.dart';
3 years ago
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;
}
3 years ago
class Location {
static Location _instance;
Location._internal() {
aMapFlutterLocation = LocationFlutterPlugin();
}
static Location getInstance() {
if (_instance == null) {
_instance = Location._internal();
}
return _instance;
}
LocationFlutterPlugin aMapFlutterLocation;
Future<bool> startLocation(context, Function(BaiduLocation result) locationCallback) async {
aMapFlutterLocation.prepareLoc(initAndroidOptions().getMap(), initIOSOptions().getMap());
3 years ago
if (!(await Permission.locationWhenInUse.serviceStatus.isEnabled)) {
enableLocation(context);
3 years ago
return false;
3 years ago
}
if (await Permission.location.isPermanentlyDenied) {
requestDialog(context, locationCallback);
3 years ago
return false;
3 years ago
} else if (await Permission.location.isGranted) {
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();
}
3 years ago
return true;
3 years ago
} else if (await Permission.location.isUndetermined) {
await Permission.location.request();
3 years ago
return false;
3 years ago
} else {
if (Platform.isIOS) {
//去设置中心
requestDialog(context, locationCallback);
3 years ago
} else {
await Permission.location.request();
}
3 years ago
return false;
3 years ago
}
}
3 years ago
void stopLocation() {
3 years ago
if (aMapFlutterLocation != null) aMapFlutterLocation.stopLocation();
3 years ago
}
enableLocation(context) {
showCupertinoDialog(
context: context,
builder: (context) {
return RequestPermission(
"assets/image/icon_permission_location_bg.webp",
3 years ago
S.of(context).nindingweigongnengweikaiqi,
S.of(context).weilexiangnintuijianfujindemendianxinxi,
S.of(context).dakaidingwei,
3 years ago
(result) async {
if (result) {
3 years ago
final AndroidIntent intent = AndroidIntent(
action: 'action_location_source_settings',
package: "com.zsw.huixiang");
await intent.launch();
// startLocation();
}
},
heightRatioWithWidth: 0.82,
);
},
);
}
requestDialog(context, Function(BaiduLocation result) locationCallback) {
3 years ago
showCupertinoDialog(
3 years ago
context: context,
builder: (context) {
return RequestPermission(
"assets/image/icon_permission_location_bg.webp",
3 years ago
S.of(context).nindingweiquanxianweiyunxu,
S.of(context).weilexiangnintuijianfujindemendianxinxi,
S.of(context).kaiqiquanxian,
(result) async {
if (result) {
await openAppSettings();
if (await Permission.location.isGranted) {
startLocation(context, locationCallback);
3 years ago
}
3 years ago
}
},
heightRatioWithWidth: 0.82,
);
},
);
3 years ago
}
}