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.
49 lines
1.4 KiB
49 lines
1.4 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',
|
|
);
|
|
|
|
// 事件通道 - 简化为只使用数据和状态通道
|
|
late final EventChannel _dataEventChannel;
|
|
late final EventChannel _statusEventChannel;
|
|
|
|
// 事件流
|
|
late final Stream<dynamic> _dataStream;
|
|
late final Stream<dynamic> _statusStream;
|
|
|
|
// 获取事件流
|
|
Stream<dynamic> get onDataReceived => _dataStream;
|
|
|
|
// 状态流直接传递原始数据
|
|
Stream<dynamic> get onStatusUpdated => _statusStream;
|
|
|
|
// 内部构造函数
|
|
LocationService._internal() {
|
|
_dataEventChannel = const EventChannel(
|
|
'com.yunqiinnovation.music_service/data_events',
|
|
);
|
|
_statusEventChannel = const EventChannel(
|
|
'com.yunqiinnovation.music_service/status_events',
|
|
);
|
|
|
|
// 初始化事件流
|
|
_dataStream = _dataEventChannel.receiveBroadcastStream();
|
|
_statusStream = _statusEventChannel.receiveBroadcastStream();
|
|
}
|
|
|
|
Future<Map<String, double>> location() async {
|
|
final result = await _methodChannel.invokeMethod<Map>('location');
|
|
return {
|
|
'latitude': result?['longitude'] ?? 0.0,
|
|
'longitude': result?['longitude'] ?? 0.0,
|
|
};
|
|
}
|
|
}
|
|
|