xbr_gaode_location 5.1.0
xbr_gaode_location: ^5.1.0 copied to clipboard
高德地图flutter地位内插件,amap_flutter,配合 xbr_gaode_amap 和 xbr_gaode_search 使用
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:xbr_gaode_location/core/location.dart';
import 'package:xbr_gaode_location/xbr_gaode_location.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
int count = 0;
LocationInfo? locationInfo;
@override
void initState() {
super.initState();
init();
}
Future<void> init() async {
//初始化 建议在App启动时完成
await XbrGaodeLocation.initKey( androidKey:"0904c47f0bd8d69ab10daf3d500e4df7", iosKey:"");
//注意这里没有做任何权限,直接启动不会成功
//显示用户协议 建议在嵌软件使用协议 用户查看协议时调用
await XbrGaodeLocation.updatePrivacy( hasContains:true, hasShow:true,hasAgree: true);
startLocation();
}
void startLocation(){
XbrGaodeLocation.instance().execOnceLocation(callback: (LocationInfo locationInfo){
print("执行单次定位已成功,即将开始后台定位");
XbrGaodeLocation.instance().startTimeLocation(interval: 16000,backgroundService: true,callback: (LocationInfo locationInfo){
setState(() {
count++;
this.locationInfo = locationInfo;
});
print("哈哈:定位回调成功:${count}次;${DateTime.now()}");
print(locationInfo?.locationTime??"-----------------------------------------------------");
//连续定位根据配置时间间隔返回
//建议在地图控件中测试 dev搜索 xbr_gaode_amap
});
});
}
void startOnceLocation(){
XbrGaodeLocation.instance().execOnceLocation(callback: (LocationInfo locationInfo){
//单次定位返回一次
//建议在地图控件中测试 dev搜索 xbr_gaode_amap
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('正在后台定位'),
),
body: Center(
child: Text('回调成功:$count次\n${locationInfo?.toJson()}'),
),
),
);
}
}