From 0266c826ae3f62e01e04115235fe26c87d87faae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=93=88=E5=93=88=E5=93=88?= Date: Wed, 19 Oct 2022 19:01:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E7=99=BE=E5=BA=A6api?= =?UTF-8?q?=EF=BC=8C=20=E4=BF=AE=E6=94=B9=E5=AE=9A=E4=BD=8D=E6=96=B9?= =?UTF-8?q?=E6=B3=95=EF=BC=8C=20=E5=A2=9E=E5=8A=A0=E7=99=BE=E5=BA=A6?= =?UTF-8?q?=E9=9A=90=E7=A7=81=E5=8D=8F=E8=AE=AE=E7=BB=9F=E4=B8=80=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/address/address_map_page.dart | 74 ++++++-------- lib/home/home_view/quick_order.dart | 40 +++----- lib/main_page.dart | 8 +- lib/mine/mine_shop_details.dart | 18 ++-- lib/mine/vip_detail_page.dart | 18 ++-- lib/order/store_selector_page.dart | 49 ++++----- .../settlement_view/distribution.dart | 24 ++--- lib/union/location_map_page.dart | 25 ++--- lib/union/union_page.dart | 40 +++----- lib/utils/location.dart | 99 +++++++++++++------ pubspec.lock | 33 ++----- pubspec.yaml | 10 +- 12 files changed, 201 insertions(+), 237 deletions(-) diff --git a/lib/address/address_map_page.dart b/lib/address/address_map_page.dart index 50d9013d..a359a54c 100644 --- a/lib/address/address_map_page.dart +++ b/lib/address/address_map_page.dart @@ -11,9 +11,7 @@ import 'package:flutter/services.dart'; import 'package:flutter_baidu_mapapi_base/flutter_baidu_mapapi_base.dart'; import 'package:flutter_baidu_mapapi_map/flutter_baidu_mapapi_map.dart'; import 'package:flutter_baidu_mapapi_utils/flutter_baidu_mapapi_utils.dart'; -import 'package:flutter_bmflocation/bdmap_location_flutter_plugin.dart'; -import 'package:flutter_bmflocation/flutter_baidu_location_android_option.dart'; -import 'package:flutter_bmflocation/flutter_baidu_location_ios_option.dart'; +import 'package:flutter_bmflocation/flutter_bmflocation.dart'; import 'package:flutter_easyloading/flutter_easyloading.dart'; import 'package:huixiang/generated/l10n.dart'; import 'package:huixiang/retrofit/data/address.dart'; @@ -61,42 +59,34 @@ class _AddressMapPage extends State { if (aMapFlutterLocation == null) { aMapFlutterLocation = LocationFlutterPlugin(); - aMapFlutterLocation.requestPermission(); - aMapFlutterLocation.onResultCallback().listen((event) { - print("event: ${jsonEncode(event)}"); - if (event != null && - event["latitude"] != null && - event["longitude"] != null) { - city = event["city"]; - BMFCoordinate latLng; - if (event["latitude"] is String && event["longitude"] is String) { - latLng = BMFCoordinate(double.tryParse(event["latitude"]), - double.tryParse(event["longitude"])); - } else { - latLng = BMFCoordinate(event["latitude"], event["longitude"]); - } - BMFCalculateUtils.coordConvert( - coordinate: latLng, - fromType: BMF_COORD_TYPE.COMMON, - toType: BMF_COORD_TYPE.BD09LL) - .then((value) { - saveLatLng(value); - if (_mapController != null) { - this.latLng = Platform.isIOS ? value : latLng; - addMarker(); - _mapController.updateMapOptions( - BMFMapOptions( - center: latLng, - zoomLevel: 15, - ), - ); - } - searchPoi(latLng); - }); - } - }); } - + aMapFlutterLocation.singleLocationCallback(callback: (BaiduLocation result){ + print("event: $result"); + if (result != null && + result.latitude != null && + result.longitude != null) { + city = result.city; + BMFCoordinate latLng = BMFCoordinate(result.latitude, result.longitude); + BMFCalculateUtils.coordConvert( + coordinate: latLng, + fromType: BMF_COORD_TYPE.COMMON, + toType: BMF_COORD_TYPE.BD09LL) + .then((value) { + saveLatLng(value); + if (_mapController != null) { + this.latLng = Platform.isIOS ? value : latLng; + addMarker(); + _mapController.updateMapOptions( + BMFMapOptions( + center: latLng, + zoomLevel: 15, + ), + ); + } + searchPoi(latLng); + }); + } + }); _setLocOption(); getLatLng(); @@ -109,12 +99,12 @@ class _AddressMapPage extends State { BaiduLocationAndroidOption androidOption = new BaiduLocationAndroidOption(); androidOption.setCoorType("GCJ02"); // 设置返回的位置坐标系类型 androidOption.setIsNeedAltitude(true); // 设置是否需要返回海拔高度信息 - androidOption.setIsNeedAddres(true); // 设置是否需要返回地址信息 + androidOption.setIsNeedAddress(true); // 设置是否需要返回地址信息 androidOption.setIsNeedLocationPoiList(true); // 设置是否需要返回周边poi信息 androidOption.setIsNeedNewVersionRgc(true); // 设置是否需要返回最新版本rgc信息 androidOption.setIsNeedLocationDescribe(true); // 设置是否需要返回位置描述 androidOption.setOpenGps(true); // 设置是否需要使用gps - androidOption.setLocationMode(LocationMode.Hight_Accuracy); // 设置定位模式 + androidOption.setLocationMode(BMFLocationMode.hightAccuracy); // 设置定位模式 androidOption.setScanspan(0); // 设置发起定位请求时间间隔 Map androidMap = androidOption.getMap(); @@ -124,9 +114,9 @@ class _AddressMapPage extends State { iosOption.setIsNeedNewVersionRgc(true); // 设置是否需要返回最新版本rgc信息 iosOption.setBMKLocationCoordinateType( "BMKLocationCoordinateTypeGCJ02"); // 设置返回的位置坐标系类型 - iosOption.setActivityType("CLActivityTypeAutomotiveNavigation"); // 设置应用位置类型 + iosOption.setActivityType(BMFActivityType.automotiveNavigation); // 设置应用位置类型 iosOption.setLocationTimeout(10); // 设置位置获取超时时间 - iosOption.setDesiredAccuracy("kCLLocationAccuracyBest"); // 设置预期精度参数 + iosOption.setDesiredAccuracy(BMFDesiredAccuracy.best); // 设置预期精度参数 iosOption.setReGeocodeTimeout(10); // 设置获取地址信息超时时间 iosOption.setDistanceFilter(1000); // 设置定位最小更新距离 iosOption.setAllowsBackgroundLocationUpdates(true); // 是否允许后台定位 diff --git a/lib/home/home_view/quick_order.dart b/lib/home/home_view/quick_order.dart index a00410ea..af65a3dc 100644 --- a/lib/home/home_view/quick_order.dart +++ b/lib/home/home_view/quick_order.dart @@ -4,6 +4,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_baidu_mapapi_base/flutter_baidu_mapapi_base.dart'; import 'package:flutter_baidu_mapapi_map/flutter_baidu_mapapi_map.dart'; import 'package:flutter_baidu_mapapi_utils/flutter_baidu_mapapi_utils.dart'; +import 'package:flutter_bmflocation/flutter_bmflocation.dart'; import 'package:flutter_svg/flutter_svg.dart'; import 'package:huixiang/generated/l10n.dart'; import 'package:huixiang/store/scan.dart'; @@ -100,37 +101,29 @@ class _QuickOrder extends State { } startLocation() async { - Location.getInstance() - .aMapFlutterLocation - .onResultCallback() - .listen((event) { - if (event != null && - event["latitude"] != null && - event["longitude"] != null) { - print("location: $event"); - if (event["latitude"] is String && event["longitude"] is String) { - latLng = BMFCoordinate(double.tryParse(event["latitude"]), - double.tryParse(event["longitude"])); - } else { - latLng = BMFCoordinate(event["latitude"], event["longitude"]); - } + Location.getInstance().startLocation(context, (BaiduLocation result){ + if (result != null && + result.latitude != null && + result.longitude != null) { + print("location: $result"); + latLng = BMFCoordinate(result.latitude, result.longitude); BMFCalculateUtils.coordConvert( - coordinate: latLng, - fromType: BMF_COORD_TYPE.BD09LL, - toType: BMF_COORD_TYPE.COMMON) + coordinate: latLng, + fromType: BMF_COORD_TYPE.BD09LL, + toType: BMF_COORD_TYPE.COMMON) .then((value) { this.latLng = value; saveLatLng( - value, event["province"], event["city"], event["district"]); + value, result.province, result.city, result.district); print("union: Location result ${value.latitude} " "${value.longitude}"); Location.getInstance().stopLocation(); queryStore( "${value.latitude}", "${value.longitude}", - event["province"], - event["city"], - event["district"], + result.province, + result.city, + result.district, editingController.text); if (_mapController != null) _mapController.updateMapOptions(BMFMapOptions( @@ -139,10 +132,7 @@ class _QuickOrder extends State { )); }); } - }); - - Location.getInstance().prepareLoc(); - Location.getInstance().startLocation(context).then((value) {}); + }).then((value) {}); } saveLatLng(BMFCoordinate latLng, province, city, district) async { diff --git a/lib/main_page.dart b/lib/main_page.dart index b4bb768b..a8088503 100644 --- a/lib/main_page.dart +++ b/lib/main_page.dart @@ -3,7 +3,7 @@ import 'dart:io'; import 'package:dio/dio.dart'; import 'package:flutter/material.dart'; import 'package:flutter_baidu_mapapi_base/flutter_baidu_mapapi_base.dart'; -import 'package:flutter_bmflocation/bdmap_location_flutter_plugin.dart'; +import 'package:flutter_bmflocation/flutter_bmflocation.dart'; import 'package:flutter_easyloading/flutter_easyloading.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:flutter_svg/flutter_svg.dart'; @@ -193,8 +193,12 @@ class _MainPage extends State with WidgetsBindingObserver { ); SharesdkPlugin.regist(shareSDKRegister); + LocationFlutterPlugin myLocPlugin = LocationFlutterPlugin(); + // // 设置是否隐私政策 + myLocPlugin.setAgreePrivacy(true); + BMFMapSDK.setAgreePrivacy(true); if (Platform.isIOS) { - LocationFlutterPlugin.setApiKey("ylW2QPlsbERkho7jOgU4GQSeawmdUIoR"); + myLocPlugin.authAK("ylW2QPlsbERkho7jOgU4GQSeawmdUIoR"); BMFMapSDK.setApiKeyAndCoordType( 'ylW2QPlsbERkho7jOgU4GQSeawmdUIoR', BMF_COORD_TYPE.COMMON, diff --git a/lib/mine/mine_shop_details.dart b/lib/mine/mine_shop_details.dart index 7d4ab3bc..3d02e9f9 100644 --- a/lib/mine/mine_shop_details.dart +++ b/lib/mine/mine_shop_details.dart @@ -1,6 +1,7 @@ import 'package:barcode_widget/barcode_widget.dart'; import 'package:dio/dio.dart'; import 'package:flutter/material.dart'; +import 'package:flutter_bmflocation/flutter_bmflocation.dart'; import 'package:flutter_easyloading/flutter_easyloading.dart'; import 'package:huixiang/generated/l10n.dart'; import 'package:huixiang/retrofit/data/base_data.dart'; @@ -83,20 +84,15 @@ class _MineShopDetails extends State { startLocation() async { // EasyLoading.show(status: S.current.zhengzaijiazai); - Location.getInstance() - .aMapFlutterLocation - .onResultCallback() - .listen((event) { - if (event != null && - event["latitude"] != null && - event["longitude"] != null) { - vipDetail(event["latitude"], event["longitude"]); + Location.getInstance().startLocation(context, (BaiduLocation result){ + if (result != null && + result.latitude != null && + result.longitude != null) { + vipDetail(result.latitude, result.longitude); } else { EasyLoading.dismiss(); } - }); - Location.getInstance().prepareLoc(); - Location.getInstance().startLocation(context).then((value) { + }).then((value) { if (!value) { EasyLoading.dismiss(); refreshController.refreshFailed(); diff --git a/lib/mine/vip_detail_page.dart b/lib/mine/vip_detail_page.dart index b04a1d6a..068c450f 100644 --- a/lib/mine/vip_detail_page.dart +++ b/lib/mine/vip_detail_page.dart @@ -1,6 +1,7 @@ import 'package:barcode_widget/barcode_widget.dart'; import 'package:dio/dio.dart'; import 'package:flutter/material.dart'; +import 'package:flutter_bmflocation/flutter_bmflocation.dart'; import 'package:flutter_easyloading/flutter_easyloading.dart'; import 'package:huixiang/generated/l10n.dart'; import 'package:huixiang/retrofit/data/base_data.dart'; @@ -50,20 +51,15 @@ class _VipDetailPage extends State { startLocation() async { // EasyLoading.show(status: S.current.zhengzaijiazai); - Location.getInstance() - .aMapFlutterLocation - .onResultCallback() - .listen((event) { - if (event != null && - event["latitude"] != null && - event["longitude"] != null) { - vipDetail(event["latitude"], event["longitude"]); + Location.getInstance().startLocation(context, (BaiduLocation result) { + if (result != null && + result.latitude != null && + result.longitude != null) { + vipDetail(result.latitude, result.longitude); } else { EasyLoading.dismiss(); } - }); - Location.getInstance().prepareLoc(); - Location.getInstance().startLocation(context).then((value) { + }).then((value) { if (!value) { EasyLoading.dismiss(); refreshController.refreshFailed(); diff --git a/lib/order/store_selector_page.dart b/lib/order/store_selector_page.dart index 971d4f74..6326bc42 100644 --- a/lib/order/store_selector_page.dart +++ b/lib/order/store_selector_page.dart @@ -6,7 +6,7 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_baidu_mapapi_base/flutter_baidu_mapapi_base.dart'; import 'package:flutter_baidu_mapapi_utils/flutter_baidu_mapapi_utils.dart'; -import 'package:flutter_bmflocation/bdmap_location_flutter_plugin.dart'; +import 'package:flutter_bmflocation/flutter_bmflocation.dart'; import 'package:flutter_easyloading/flutter_easyloading.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:huixiang/generated/l10n.dart'; @@ -52,33 +52,27 @@ class _StoreSelectorPage extends State { if (aMapFlutterLocation == null) { aMapFlutterLocation = LocationFlutterPlugin(); - aMapFlutterLocation.onResultCallback().listen((event) { - if (event != null && - event["latitude"] != null && - event["longitude"] != null) { - print("location: $event"); - aMapFlutterLocation.stopLocation(); - if (event["latitude"] is String && event["longitude"] is String) { - latLng = BMFCoordinate(double.tryParse(event["latitude"]), - double.tryParse(event["longitude"])); - } else { - latLng = BMFCoordinate(event["latitude"], event["longitude"]); - } - BMFCalculateUtils.coordConvert( - coordinate: latLng, - fromType: BMF_COORD_TYPE.COMMON, - toType: BMF_COORD_TYPE.BD09LL) - .then((value) { - this.latLng = value; - saveLatLng( - value, event["province"], event["city"], event["district"]); - queryStore("${value.latitude}", "${value.longitude}", - event["province"], event["city"], event["district"]); - }); - } - }); } - + aMapFlutterLocation.singleLocationCallback(callback: (BaiduLocation result) { + if (result != null && + result.latitude != null && + result.longitude != null) { + print("location: $result"); + aMapFlutterLocation.stopLocation(); + latLng = BMFCoordinate(result.latitude, result.longitude); + BMFCalculateUtils.coordConvert( + coordinate: latLng, + fromType: BMF_COORD_TYPE.COMMON, + toType: BMF_COORD_TYPE.BD09LL) + .then((value) { + this.latLng = value; + saveLatLng( + value, result.province, result.city, result.district); + queryStore("${value.latitude}", "${value.longitude}", + result.province, result.city, result.district); + }); + } + }); aMapFlutterLocation.prepareLoc({ "coorType": "bd09ll", "isNeedAddres": false, @@ -95,7 +89,6 @@ class _StoreSelectorPage extends State { "reGeocodeTimeout": 10, "activityType": "CLActivityTypeAutomotiveNavigation", "BMKLocationCoordinateType": "BMKLocationCoordinateTypeBMK09LL", - "BMKLocationCoordinateType": "BMKLocationCoordinateTypeBMK09LL", "isNeedNewVersionRgc": false, }); diff --git a/lib/settlement/settlement_view/distribution.dart b/lib/settlement/settlement_view/distribution.dart index dd5cb8cb..2abd5a15 100644 --- a/lib/settlement/settlement_view/distribution.dart +++ b/lib/settlement/settlement_view/distribution.dart @@ -1,5 +1,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_baidu_mapapi_base/flutter_baidu_mapapi_base.dart'; +import 'package:flutter_baidu_mapapi_map/flutter_baidu_mapapi_map.dart'; +import 'package:flutter_bmflocation/flutter_bmflocation.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:flutter_svg/flutter_svg.dart'; import 'package:huixiang/generated/l10n.dart'; @@ -54,26 +56,16 @@ class _DistributionMode extends State { ///定位获取当前的位置 void startLocation() async { - Location.getInstance() - .aMapFlutterLocation - .onResultCallback() - .listen((event) { - if (event != null && - event["latitude"] != null && - event["longitude"] != null) { - print("location: $event"); - if (event["latitude"] is String && event["longitude"] is String) { - myLatLng = BMFCoordinate(double.tryParse(event["latitude"]), - double.tryParse(event["longitude"])); - } else { - myLatLng = BMFCoordinate(event["latitude"], event["longitude"]); - } + Location.getInstance().startLocation(context, (BaiduLocation result){ + if (result != null && + result.latitude != null && + result.longitude != null) { + print("location: $result"); + myLatLng = BMFCoordinate(result.latitude, result.longitude); calculate(); Location.getInstance().stopLocation(); } }); - Location.getInstance().prepareLoc(); - Location.getInstance().startLocation(context); } calculate() async { diff --git a/lib/union/location_map_page.dart b/lib/union/location_map_page.dart index caa3aa1e..0f37fd49 100644 --- a/lib/union/location_map_page.dart +++ b/lib/union/location_map_page.dart @@ -2,6 +2,7 @@ import 'dart:ui'; import 'package:flutter/material.dart'; import 'package:flutter_baidu_mapapi_base/flutter_baidu_mapapi_base.dart'; import 'package:flutter_baidu_mapapi_map/flutter_baidu_mapapi_map.dart'; +import 'package:flutter_bmflocation/flutter_bmflocation.dart'; import 'package:huixiang/utils/flutter_utils.dart'; import 'package:huixiang/utils/location.dart'; import 'package:huixiang/view_widget/my_appbar.dart'; @@ -26,29 +27,19 @@ class _LocationMap extends State { startLocation(); } - void startLocation() async { - Location.getInstance() - .aMapFlutterLocation - .onResultCallback() - .listen((event) { - if (event != null && - event["latitude"] != null && - event["longitude"] != null) { - print("location: $event"); - if (event["latitude"] is String && event["longitude"] is String) { - myLatLng = BMFCoordinate(double.tryParse(event["latitude"]), - double.tryParse(event["longitude"])); - } else { - myLatLng = BMFCoordinate(event["latitude"], event["longitude"]); - } + void startLocation() { + Location.getInstance().startLocation(context, (BaiduLocation result) { + if (result != null && + result.latitude != null && + result.longitude != null) { + print("location: $result"); + myLatLng = BMFCoordinate(result.latitude, result.longitude); AppUtils.coordConvert(myLatLng).then((value) { this.myLatLng = value; locationShow(); }); } }); - Location.getInstance().prepareLoc(); - Location.getInstance().startLocation(context); } locationShow() { diff --git a/lib/union/union_page.dart b/lib/union/union_page.dart index 44caea6b..3cf6eaa7 100644 --- a/lib/union/union_page.dart +++ b/lib/union/union_page.dart @@ -6,6 +6,7 @@ import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_baidu_mapapi_map/flutter_baidu_mapapi_map.dart'; import 'package:flutter_baidu_mapapi_utils/flutter_baidu_mapapi_utils.dart'; +import 'package:flutter_bmflocation/flutter_bmflocation.dart'; import 'package:flutter_easyloading/flutter_easyloading.dart'; import 'package:huixiang/generated/l10n.dart'; import 'package:huixiang/main.dart'; @@ -130,37 +131,29 @@ class UnionPageState extends State startLocation(bool isOnRefresh) async { if (!isOnRefresh) EasyLoading.show(status: S.current.zhengzaijiazai); - Location.getInstance() - .aMapFlutterLocation - .onResultCallback() - .listen((event) { - if (event != null && - event["latitude"] != null && - event["longitude"] != null) { - print("location: $event"); - if (event["latitude"] is String && event["longitude"] is String) { - latLng = BMFCoordinate(double.tryParse(event["latitude"]), - double.tryParse(event["longitude"])); - } else { - latLng = BMFCoordinate(event["latitude"], event["longitude"]); - } + Location.getInstance().startLocation(context, (BaiduLocation result) { + if (result != null && + result.latitude != null && + result.longitude != null) { + print("location: $result"); + latLng = BMFCoordinate(result.latitude, result.longitude); BMFCalculateUtils.coordConvert( - coordinate: latLng, - fromType: BMF_COORD_TYPE.BD09LL, - toType: BMF_COORD_TYPE.COMMON) + coordinate: latLng, + fromType: BMF_COORD_TYPE.BD09LL, + toType: BMF_COORD_TYPE.COMMON) .then((value) { this.latLng = value; saveLatLng( - value, event["province"], event["city"], event["district"]); + value, result.province, result.city, result.district); print("union: Location result ${value.latitude} " "${value.longitude}"); Location.getInstance().stopLocation(); queryStore( "${value.latitude}", "${value.longitude}", - event["province"], - event["city"], - event["district"], + result.province, + result.city, + result.district, "", -1); if (_mapController != null) @@ -173,10 +166,7 @@ class UnionPageState extends State getLatLng(); // EasyLoading.dismiss(); } - }); - - Location.getInstance().prepareLoc(); - Location.getInstance().startLocation(context).then((value) { + }).then((value) { if (!value) { EasyLoading.dismiss(); tabRefresh().refreshFailed(); diff --git a/lib/utils/location.dart b/lib/utils/location.dart index 5f7eb972..5dfa092b 100644 --- a/lib/utils/location.dart +++ b/lib/utils/location.dart @@ -2,11 +2,64 @@ import 'dart:io'; import 'package:android_intent_plus/android_intent.dart'; import 'package:flutter/cupertino.dart'; -import 'package:flutter_bmflocation/bdmap_location_flutter_plugin.dart'; +import 'package:flutter_bmflocation/flutter_bmflocation.dart'; import 'package:huixiang/generated/l10n.dart'; import 'package:huixiang/view_widget/request_permission.dart'; import 'package:permission_handler/permission_handler.dart'; + +BaiduLocationAndroidOption initAndroidOptions() { + BaiduLocationAndroidOption options = BaiduLocationAndroidOption( + // 定位模式,可选的模式有高精度、仅设备、仅网络。默认为高精度模式 + locationMode: BMFLocationMode.hightAccuracy, + // 是否需要返回地址信息 + isNeedAddress: true, +// 是否需要返回海拔高度信息 + isNeedAltitude: false, +// 是否需要返回周边poi信息 + isNeedLocationPoiList: false, +// 是否需要返回新版本rgc信息 + isNeedNewVersionRgc: false, +// 是否需要返回位置描述信息 + isNeedLocationDescribe: true, +// 是否使用gps + openGps: true, +// 可选,设置场景定位参数,包括签到场景、运动场景、出行场景 + locationPurpose: BMFLocationPurpose.signIn, +// 坐标系 + coordType: BMFLocationCoordType.bd09ll, +// 设置发起定位请求的间隔,int类型,单位ms +// 如果设置为0,则代表单次定位,即仅定位一次,默认为0 + scanspan: 0); + return options; +} + +BaiduLocationIOSOption initIOSOptions() { + BaiduLocationIOSOption options = BaiduLocationIOSOption( + // 坐标系 + coordType: BMFLocationCoordType.bd09ll, + // 位置获取超时时间 + locationTimeout: 10, + // 获取地址信息超时时间 + reGeocodeTimeout: 10, + // 应用位置类型 默认为automotiveNavigation + activityType: BMFActivityType.automotiveNavigation, + // 设置预期精度参数 默认为best + desiredAccuracy: BMFDesiredAccuracy.best, + // 是否需要最新版本rgc数据 + isNeedNewVersionRgc: false, + // 指定定位是否会被系统自动暂停 + pausesLocationUpdatesAutomatically: false, + // 指定是否允许后台定位, + // 允许的话是可以进行后台定位的,但需要项目配置允许后台定位,否则会报错,具体参考开发文档 + allowsBackgroundLocationUpdates: false, + // 设定定位的最小更新距离 + distanceFilter: 10, + ); + return options; +} + + class Location { static Location _instance; @@ -23,37 +76,27 @@ class Location { LocationFlutterPlugin aMapFlutterLocation; - prepareLoc() { - aMapFlutterLocation.prepareLoc({ - "coorType": "bd09ll", - "isNeedAddres": false, - "isNeedAltitude": false, - "isNeedLocationPoiList": false, - "isNeedLocationDescribe": false, - "isNeedNewVersionRgc": false, - "scanspan": 0, - "openGps": true, - "locationMode": 2, - }, { - "locationMode": "kCLLocationAccuracyBest", - "locationTimeout": 10, - "reGeocodeTimeout": 10, - "activityType": "CLActivityTypeAutomotiveNavigation", - "BMKLocationCoordinateType": "BMKLocationCoordinateTypeBMK09LL", - "isNeedNewVersionRgc": false, - }); - } - - Future startLocation(context) async { + Future startLocation(context, Function(BaiduLocation result) locationCallback) async { + aMapFlutterLocation.prepareLoc(initAndroidOptions().getMap(), initIOSOptions().getMap()); if (!(await Permission.locationWhenInUse.serviceStatus.isEnabled)) { enableLocation(context); return false; } if (await Permission.location.isPermanentlyDenied) { - requestDialog(context); + requestDialog(context, locationCallback); return false; } else if (await Permission.location.isGranted) { - aMapFlutterLocation.startLocation(); + aMapFlutterLocation.singleLocationCallback(callback: (BaiduLocation result) { + locationCallback.call(result); + }); + if (Platform.isIOS) { + await aMapFlutterLocation.singleLocation({ + 'isReGeocode': true, + 'isNetworkState': true, + }); + } else if (Platform.isAndroid) { + await aMapFlutterLocation.startLocation(); + } return true; } else if (await Permission.location.isUndetermined) { await Permission.location.request(); @@ -61,7 +104,7 @@ class Location { } else { if (Platform.isIOS) { //去设置中心 - requestDialog(context); + requestDialog(context, locationCallback); } else { await Permission.location.request(); } @@ -97,7 +140,7 @@ class Location { ); } - requestDialog(context) { + requestDialog(context, Function(BaiduLocation result) locationCallback) { showCupertinoDialog( context: context, builder: (context) { @@ -110,7 +153,7 @@ class Location { if (result) { await openAppSettings(); if (await Permission.location.isGranted) { - startLocation(context); + startLocation(context, locationCallback); } } }, diff --git a/pubspec.lock b/pubspec.lock index e80cc94b..b1f43fda 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -92,20 +92,6 @@ packages: url: "https://pub.flutter-io.cn" source: hosted version: "1.16.0" - convert: - dependency: transitive - description: - name: convert - url: "https://pub.flutter-io.cn" - source: hosted - version: "2.1.1" - crypto: - dependency: transitive - description: - name: crypto - url: "https://pub.flutter-io.cn" - source: hosted - version: "2.1.5" csslib: dependency: transitive description: @@ -173,35 +159,35 @@ packages: name: flutter_baidu_mapapi_base url: "https://pub.flutter-io.cn" source: hosted - version: "2.0.1" + version: "3.2.0" flutter_baidu_mapapi_map: dependency: "direct main" description: name: flutter_baidu_mapapi_map url: "https://pub.flutter-io.cn" source: hosted - version: "2.0.1" + version: "3.2.1" flutter_baidu_mapapi_search: dependency: "direct main" description: name: flutter_baidu_mapapi_search url: "https://pub.flutter-io.cn" source: hosted - version: "2.0.1" + version: "3.2.0" flutter_baidu_mapapi_utils: dependency: "direct main" description: name: flutter_baidu_mapapi_utils url: "https://pub.flutter-io.cn" source: hosted - version: "2.0.1" + version: "3.2.0" flutter_bmflocation: dependency: "direct main" description: name: flutter_bmflocation url: "https://pub.flutter-io.cn" source: hosted - version: "1.0.4" + version: "3.2.1" flutter_easyloading: dependency: "direct main" description: @@ -248,7 +234,7 @@ packages: name: flutter_plugin_android_lifecycle url: "https://pub.flutter-io.cn" source: hosted - version: "1.0.11" + version: "2.0.7" flutter_screenutil: dependency: "direct main" description: @@ -819,13 +805,6 @@ packages: url: "https://pub.flutter-io.cn" source: hosted version: "0.0.1+3" - uuid: - dependency: transitive - description: - name: uuid - url: "https://pub.flutter-io.cn" - source: hosted - version: "2.2.2" vector_math: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index dc46b24e..5306629c 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -37,11 +37,11 @@ dependencies: permission_handler: ^5.0.1+1 - flutter_bmflocation: ^1.0.3 - flutter_baidu_mapapi_base: ^2.0.1 - flutter_baidu_mapapi_map: ^2.0.1 - flutter_baidu_mapapi_search: ^2.0.1 - flutter_baidu_mapapi_utils: ^2.0.1 + flutter_bmflocation: ^3.2.1 + flutter_baidu_mapapi_base: ^3.2.0 + flutter_baidu_mapapi_map: ^3.2.1 + flutter_baidu_mapapi_search: ^3.2.0 + flutter_baidu_mapapi_utils: ^3.2.0 dio: ^3.0.10 rxdart: ^0.26.0