gmm_amap_flutter_location
高德地图 Flutter 插件 - 定位功能(已适配 Flutter 3.35+ / Dart 3.9+)
本插件是基于 amap_flutter_location 的改进版本,修复了在新版本 Flutter/Dart 上的兼容性问题。
功能特性
- ✅ 支持获取设备当前位置
- ✅ 支持连续定位和单次定位
- ✅ 支持 Flutter 3.35+ 和 Dart 3.9+
- ✅ 支持 Android 和 iOS 平台
安装
在 pubspec.yaml 中添加依赖:
dependencies:
gmm_amap_flutter_location: ^3.1.3
然后运行:
flutter pub get
平台配置
Android 配置
- 在
android/app/src/main/AndroidManifest.xml中添加权限和配置:
<!-- 定位权限 -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application>
<!-- 高德定位服务 -->
<service android:name="com.amap.api.location.APSService" />
<!-- 高德 API Key -->
<meta-data
android:name="com.amap.api.v2.apikey"
android:value="您的Android Key" />
</application>
iOS 配置
- 在
ios/Runner/Info.plist中添加定位权限描述:
<key>NSLocationWhenInUseUsageDescription</key>
<string>需要获取您的位置以提供定位服务</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>需要获取您的位置以提供定位服务</string>
- 在
ios/Runner/AppDelegate.swift中配置 API Key:
import AMapFoundationKit
// 在 application:didFinishLaunchingWithOptions: 中添加
AMapServices.shared().apiKey = "您的iOS Key"
使用示例
初始化定位
import 'package:gmm_amap_flutter_location/gmm_amap_flutter_location.dart';
class LocationService {
final AMapFlutterLocation _location = AMapFlutterLocation();
void init() {
// 设置 API Key
AMapFlutterLocation.setApiKey(
androidKey: '您的Android Key',
iosKey: '您的iOS Key',
);
// 设置隐私合规
AMapFlutterLocation.updatePrivacyAgree(true);
AMapFlutterLocation.updatePrivacyShow(true, true);
}
}
开始定位
// 监听定位结果
_location.onLocationChanged().listen((Map<String, Object> result) {
print('定位结果: $result');
final latitude = result['latitude'];
final longitude = result['longitude'];
final address = result['address'];
print('坐标: $latitude, $longitude');
print('地址: $address');
});
// 开始定位
_location.startLocation();
停止定位
// 停止定位
_location.stopLocation();
// 销毁定位
_location.destroy();
相关插件
- gmm_amap_flutter_base - 高德地图基础库
- gmm_amap_flutter_map - 高德地图组件
许可证
MIT License - 详见 LICENSE 文件
致谢
本插件基于高德官方 amap_flutter_location 改进,感谢高德开放平台的支持。