前言:
国内flutter开发使用google地图不方便,目前只有高德支持flutter,虽然功能不是很全,但是基本的地图、定位、导航功能已经有了。
安装及使用:
1 pubspec.yaml文件中添加
amap_base: ^0.2.12
之后运行
flutter packages get
2 获取发布版和调试版SHA1,参考flutter填坑记录(更新中)的第七个问题
3 申请高德地图key,官网步骤
4 配置key
<application
android:name="io.flutter.app.FlutterApplication"
android:label="flutter_ui_framework"
android:icon="@mipmap/ic_launcher">
<meta-data
android:name="com.amap.api.v2.apikey"
android:value="39d5d4f31ea7f3993e7a0c2824c71ec5"/>//你的key
......
5 示例代码,定位获取经纬度
import 'package:amap_base/amap_base.dart';
final _amapLocation = AMapLocation();
var _result = '';
//初始化定位监听
void _initLocation() async {
_amapLocation.init();
final options = LocationClientOptions(
isOnceLocation: false,
locatingWithReGeocode: true,
);
if (await Permissions().requestPermission()) {
_amapLocation.startLocate(options).listen((_) => setState(() {
_result =
'坐标:${_.longitude},${_.latitude} @ ${DateTime.now().hour}:${DateTime.now().minute}:${DateTime.now().second}';
print(_result);
}));
} else {
setState(() {
_result = "无定位权限";
});
}
}