Flutter Location包onLocationChanged在iOS上始终返回相同坐标求助
iOS端location ^4.4.0包监听位置始终返回相同坐标的解决建议
我使用location ^4.4.0包监听位置变化,Android端运行正常,但iOS上始终返回相同的坐标。已完成iOS端包配置,通过permission_handler获取了权限,测试设备是搭载iOS 16.3的iPhone XR,监听能正常获取数据但坐标从未更新。
相关代码
Location location = Location(); location.onLocationChanged.listen((LocationData currentLocation) { print('{"latitude": "${currentLocation.latitude}", "longitude": "${currentLocation.longitude}"}'); });
flutter doctor -v 输出
** flutter doctor -v ** [✓] Flutter (Channel stable, 3.7.3, on macOS 12.6 21G115 darwin-x64, locale en-PT) • Flutter version 3.7.3 on channel stable at /Users/tiago/Code/flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision 9944297138 (6 days ago), 2023-02-08 15:46:04 -0800 • Engine revision 248290d6d5 • Dart version 2.19.2 • DevTools version 2.20.1 [✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3) • Android SDK at /Users/tiago/Library/Android/sdk • Platform android-33, build-tools 30.0.3 • ANDROID_HOME = /Users/tiago/Android/Sdk • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS (Xcode 14.0.1) • Xcode at /Applications/Xcode.app/Contents/Developer • Build 14A400 • CocoaPods version 1.11.3 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] Android Studio (version 2021.3) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866) [✓] VS Code (version 1.75.0) • VS Code at /Applications/Visual Studio Code.app/Contents
解决建议
- 调整位置精度与过滤参数:iOS默认定位精度可能偏低,手动设置高精度模式并添加距离过滤,确保移动到指定距离才触发更新:
await location.changeSettings( accuracy: LocationAccuracy.high, distanceFilter: 10, // 单位:米 ); - 确认权限类型与配置:后台监听位置需要
always权限,检查Info.plist是否添加了NSLocationAlwaysAndWhenInUseUsageDescription和NSLocationWhenInUseUsageDescription字段,申请权限时明确请求Permission.locationAlways。 - 检查设备定位设置:进入iPhone设置→隐私与安全性→定位服务→你的应用,确认"精确位置"已开启,关闭该选项会返回模糊坐标且无法实时更新。
- 重启应用与设备:iOS定位服务偶尔会出现进程缓存异常,重启后可恢复正常。
- 完善初始化流程:在监听位置前,确保已完成定位服务可用性检查与权限申请:
// 检查定位服务是否开启 bool serviceEnabled = await location.serviceEnabled(); if (!serviceEnabled) { serviceEnabled = await location.requestService(); if (!serviceEnabled) return; } // 检查并申请权限 PermissionStatus permissionGranted = await location.hasPermission(); if (permissionGranted == PermissionStatus.denied) { permissionGranted = await location.requestPermission(); if (permissionGranted != PermissionStatus.granted) return; } - 升级location包:
location ^4.4.0版本较旧,升级到最新稳定版,大概率修复了iOS 16相关的定位兼容性问题。
内容的提问来源于stack exchange,提问作者JS_LnMstr
相关产品推荐
相关产品推荐

