Browse Source

ai聊天增加获取当前经纬度

newdev_shunjiawei
tanlongsheng 2 years ago
parent
commit
d150692ae0
  1. 1
      amap_flutter/android/src/main/java/com/example/amap_flutter/AmapFlutterPlugin.java
  2. 27
      lib/modules/chat/controllers/chat_controller.dart

1
amap_flutter/android/src/main/java/com/example/amap_flutter/AmapFlutterPlugin.java

@ -152,6 +152,7 @@ public class AmapFlutterPlugin implements FlutterPlugin, MethodCallHandler {
Map<String, Object> locationData = new HashMap<>(); Map<String, Object> locationData = new HashMap<>();
locationData.put("latitude", amapLocation.getLatitude()); locationData.put("latitude", amapLocation.getLatitude());
locationData.put("longitude", amapLocation.getLongitude()); locationData.put("longitude", amapLocation.getLongitude());
locationData.put("city", amapLocation.getCity());
locationData.put("address", amapLocation.getAddress()); locationData.put("address", amapLocation.getAddress());
eventSink.success(locationData); eventSink.success(locationData);
} }

27
lib/modules/chat/controllers/chat_controller.dart

@ -1,4 +1,5 @@
import 'dart:async'; import 'dart:async';
import 'package:amap_flutter/amap_flutter.dart';
import 'package:deep_voice/data/services/spotify_service.dart'; import 'package:deep_voice/data/services/spotify_service.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
@ -32,6 +33,11 @@ class ChatController extends GetxController {
// 添加ScrollController用于控制列表滚动 // 添加ScrollController用于控制列表滚动
final ScrollController scrollController = ScrollController(); final ScrollController scrollController = ScrollController();
final _amapFlutter = AmapFlutter();
StreamSubscription? _locationStream;
String _latLng = '';
String _city = '';
// Agent 信息 // Agent 信息
late final Agent agent; late final Agent agent;
@ -170,6 +176,8 @@ class ChatController extends GetxController {
// 订阅VoiceInteractionService事件 // 订阅VoiceInteractionService事件
_subscribeToVoiceInteractionEvents(); _subscribeToVoiceInteractionEvents();
_getLocation();
player.playerStateStream.listen((state) async { player.playerStateStream.listen((state) async {
if (state.processingState == ProcessingState.completed) { if (state.processingState == ProcessingState.completed) {
await player.seek(Duration.zero); await player.seek(Duration.zero);
@ -184,6 +192,16 @@ class ChatController extends GetxController {
}); });
} }
void _getLocation() async {
await _amapFlutter.initLocation();
_locationStream = _amapFlutter.locationStream().listen((data) async {
_latLng = '${data['longitude']},${data['latitude']}}';
_city = data['city'];
_amapFlutter.stopLocation();
});
_amapFlutter.startLocation();
}
/// 订阅VoiceInteractionService事件 /// 订阅VoiceInteractionService事件
void _subscribeToVoiceInteractionEvents() { void _subscribeToVoiceInteractionEvents() {
if (_voiceInteractionService.eventStream != null) { if (_voiceInteractionService.eventStream != null) {
@ -265,6 +283,9 @@ class ChatController extends GetxController {
_autoStopTimer?.cancel(); _autoStopTimer?.cancel();
_scrollDebounceTimer?.cancel(); // 清理防抖计时器 _scrollDebounceTimer?.cancel(); // 清理防抖计时器
_amapFlutter.dispose();
_locationStream?.cancel();
// 停止TTS // 停止TTS
_ttsService.stop(); _ttsService.stop();
@ -432,6 +453,12 @@ class ChatController extends GetxController {
'role': m.role, 'role': m.role,
'content': m.content, 'content': m.content,
}); });
if (i == recentMessages.length - 2) {
messageHistory.add({
'role': 'user',
'content': '我当前在$_city,我的经纬度是$_latLng',
});
}
} }
return messageHistory; return messageHistory;

Loading…
Cancel
Save