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.

84 lines
2.5 KiB

import 'package:flutter/services.dart';
class LocationService {
static final LocationService _instance = LocationService._internal();
/// 获取单例
factory LocationService() => _instance;
// 方法通道
final MethodChannel _methodChannel = MethodChannel(
'com.yunqiinnovation.location_service',
);
// 内部构造函数
LocationService._internal();
/// 检查定位权限
Future<Map<String, bool>> checkPermission() async {
try {
final result = await _methodChannel.invokeMethod<Map>('checkPermission');
return {
'hasPermission': result?['hasPermission'] ?? false,
'hasForegroundPermission': result?['hasForegroundPermission'] ?? false,
'hasBackgroundPermission': result?['hasBackgroundPermission'] ?? false,
};
} catch (e) {
throw Exception('检查权限失败: $e');
}
}
/// 请求定位权限
Future<bool> requestPermission() async {
try {
final result = await _methodChannel.invokeMethod<Map>(
'requestPermission',
);
return result?['granted'] ?? false;
} catch (e) {
throw Exception('请求权限失败: $e');
}
}
/// 检查系统位置服务是否开启
/// 参数:无
/// 返回值:true 表示已开启;false 表示未开启或检测失败
/// 异常:抛出 Exception,表示方法通道调用失败或平台异常
Future<bool> isLocationServiceEnabled() async {
try {
final enabled = await _methodChannel.invokeMethod<bool>(
'isLocationServiceEnabled',
);
return enabled ?? false;
} catch (e) {
throw Exception('检查位置服务失败: $e');
}
}
/// 获取当前位置(按需获取)
Future<Map<String, dynamic>> location() async {
try {
// 先检查权限
final permissionStatus = await checkPermission();
if (!permissionStatus['hasPermission']!) {
// 请求权限
final granted = await requestPermission();
if (!granted) {
throw Exception('用户拒绝了定位权限');
}
}
final result = await _methodChannel.invokeMethod<Map>('location');
if (result != null) {
return {
'latitude': double.tryParse(result['user_lat'] ?? '0.0') ?? 0.0,
'longitude': double.tryParse(result['user_lng'] ?? '0.0') ?? 0.0,
'address': result['user_location'] ?? '未知地址',
};
}
throw Exception('获取位置信息失败');
} catch (e) {
throw Exception('定位失败: $e');
}
}
}