From b4908ceb4c6e646014e07ace4666cc42bcafa236 Mon Sep 17 00:00:00 2001 From: zhuce <821293938@qq.com> Date: Sat, 16 Mar 2024 15:52:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9A=E4=BD=8D=E6=9D=83=E9=99=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ios/Podfile | 68 ++++++++++++++- ios/Podfile.lock | 8 +- ios/Runner/Info.plist | 8 ++ lib/const_config.dart | 7 +- lib/pages/home/home_page.dart | 58 ------------- lib/pages/home/widgets/map_widget.dart | 115 +++++++++++++++++++++++++ lib/pages/login/login_page.dart | 3 + pubspec.lock | 48 +++++++++++ pubspec.yaml | 1 + 9 files changed, 254 insertions(+), 62 deletions(-) diff --git a/ios/Podfile b/ios/Podfile index d97f17e..0daa28d 100644 --- a/ios/Podfile +++ b/ios/Podfile @@ -37,8 +37,74 @@ target 'Runner' do end end + post_install do |installer| installer.pods_project.targets.each do |target| flutter_additional_ios_build_settings(target) + + # Start of the permission_handler configuration + target.build_configurations.each do |config| + + # You can enable the permissions needed here. For example to enable camera + # permission, just remove the `#` character in front so it looks like this: + # + # ## dart: PermissionGroup.camera + # 'PERMISSION_CAMERA=1' + # + # Preprocessor definitions can be found at: https://github.com/Baseflow/flutter-permission-handler/blob/master/permission_handler_apple/ios/Classes/PermissionHandlerEnums.h + config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [ + '$(inherited)', + + ## dart: [PermissionGroup.calendarWriteOnly, PermissionGroup.calendar (iOS 16 and below)] + # 'PERMISSION_EVENTS=1', + + ## dart: [PermissionGroup.calendarFullAccess, PermissionGroup.calendar (iOS 17 and above)] + # 'PERMISSION_EVENTS_FULL_ACCESS=1', + + ## dart: PermissionGroup.reminders + # 'PERMISSION_REMINDERS=1', + + ## dart: PermissionGroup.contacts + # 'PERMISSION_CONTACTS=1', + + ## dart: PermissionGroup.camera + 'PERMISSION_CAMERA=1', + + ## dart: PermissionGroup.microphone + # 'PERMISSION_MICROPHONE=1', + + ## dart: PermissionGroup.speech + # 'PERMISSION_SPEECH_RECOGNIZER=1', + + ## dart: PermissionGroup.photos + 'PERMISSION_PHOTOS=1', + + ## dart: PermissionGroup.photosAddOnly + # 'PERMISSION_PHOTOS_ADD_ONLY=1', + + ## dart: [PermissionGroup.location, PermissionGroup.locationAlways, PermissionGroup.locationWhenInUse] + 'PERMISSION_LOCATION=1', + + ## dart: PermissionGroup.notification + 'PERMISSION_NOTIFICATIONS=1', + + ## dart: PermissionGroup.mediaLibrary + # 'PERMISSION_MEDIA_LIBRARY=1', + + ## dart: PermissionGroup.sensors + # 'PERMISSION_SENSORS=1', + + ## dart: PermissionGroup.bluetooth + # 'PERMISSION_BLUETOOTH=1', + + ## dart: PermissionGroup.appTrackingTransparency + # 'PERMISSION_APP_TRACKING_TRANSPARENCY=1', + + ## dart: PermissionGroup.criticalAlerts + # 'PERMISSION_CRITICAL_ALERTS=1' + ] + + end + # End of the permission_handler configuration end -end +end \ No newline at end of file diff --git a/ios/Podfile.lock b/ios/Podfile.lock index 10268b3..955bfc9 100644 --- a/ios/Podfile.lock +++ b/ios/Podfile.lock @@ -20,6 +20,8 @@ PODS: - ZXSDK - GTSDK (3.0.5.0): - GTCommonSDK (> 3.0.9.0) + - permission_handler_apple (9.3.0): + - Flutter - shared_preferences_foundation (0.0.1): - Flutter - FlutterMacOS @@ -31,6 +33,7 @@ DEPENDENCIES: - device_info_plus (from `.symlinks/plugins/device_info_plus/ios`) - Flutter (from `Flutter`) - getuiflut (from `.symlinks/plugins/getuiflut/ios`) + - permission_handler_apple (from `.symlinks/plugins/permission_handler_apple/ios`) - shared_preferences_foundation (from `.symlinks/plugins/shared_preferences_foundation/darwin`) SPEC REPOS: @@ -53,6 +56,8 @@ EXTERNAL SOURCES: :path: Flutter getuiflut: :path: ".symlinks/plugins/getuiflut/ios" + permission_handler_apple: + :path: ".symlinks/plugins/permission_handler_apple/ios" shared_preferences_foundation: :path: ".symlinks/plugins/shared_preferences_foundation/darwin" @@ -67,9 +72,10 @@ SPEC CHECKSUMS: getuiflut: 8628ba2b64a738a847a07d2b67e0ac305778e2ee GTCommonSDK: 2be8c2d5b094929f81b64dba8767bf110bae78de GTSDK: cc04d169c5c6cb8f8884849153eaac9e6ccfdd7c + permission_handler_apple: 036b856153a2b1f61f21030ff725f3e6fece2b78 shared_preferences_foundation: b4c3b4cddf1c21f02770737f147a3f5da9d39695 ZXSDK: 414bed508b670437a9bdf7c75d49816b8cb7b2d4 -PODFILE CHECKSUM: 819463e6a0290f5a72f145ba7cde16e8b6ef0796 +PODFILE CHECKSUM: b2ff02a7210c740947b0635c5db14e8d36382e1f COCOAPODS: 1.15.2 diff --git a/ios/Runner/Info.plist b/ios/Runner/Info.plist index 32bcbab..d3f3705 100644 --- a/ios/Runner/Info.plist +++ b/ios/Runner/Info.plist @@ -45,5 +45,13 @@ UIApplicationSupportsIndirectInputEvents + NSLocationAlwaysAndWhenInUseUsageDescription + 需要使用您的定位,地图附近显示停车、车辆信息 + NSLocationAlwaysUsageDescription + 需要使用您的定位,地图附近显示停车、车辆信息 + NSLocationUsageDescription + 需要使用您的定位,地图附近显示停车、车辆信息 + NSLocationWhenInUseUsageDescription + 需要使用您的定位,地图附近显示停车、车辆信息 diff --git a/lib/const_config.dart b/lib/const_config.dart index 3eaf32a..2e26629 100644 --- a/lib/const_config.dart +++ b/lib/const_config.dart @@ -2,13 +2,16 @@ import 'package:amap_flutter_base/amap_flutter_base.dart'; import 'package:amap_flutter_map/amap_flutter_map.dart'; class ConstConfig { + + static const String androidKey='c715f41c63e5bd7faef2b53de7438709'; + static const String iosKey='8c545f5186ddd9d2c585693f72b4790a'; ///配置您申请的apikey,在此处配置之后,可以在初始化[AMapWidget]时,通过`apiKey`属性设置 /// ///注意:使用[AMapWidget]的`apiKey`属性设置的key的优先级高于通过Native配置key的优先级, ///使用[AMapWidget]的`apiKey`属性配置后Native配置的key将失效,请根据实际情况选择使用 static const AMapApiKey amapApiKeys = AMapApiKey( - androidKey: 'c715f41c63e5bd7faef2b53de7438709', - iosKey: '8c545f5186ddd9d2c585693f72b4790a'); + androidKey: androidKey, + iosKey: iosKey); ///高德隐私合规声明,这里只是示例,实际使用中请按照实际参数设置[AMapPrivacyStatement]的'hasContains''hasShow''hasAgree'这三个参数 /// diff --git a/lib/pages/home/home_page.dart b/lib/pages/home/home_page.dart index 620d289..aeb14cb 100644 --- a/lib/pages/home/home_page.dart +++ b/lib/pages/home/home_page.dart @@ -1,6 +1,4 @@ -import 'package:amap_flutter_map/amap_flutter_map.dart'; import 'package:autosos_flutter/config/theme_colors.dart'; -import 'package:autosos_flutter/const_config.dart'; import 'package:autosos_flutter/pages/home/widgets/image_block.dart'; import 'package:autosos_flutter/pages/home/widgets/map_widget.dart'; import 'package:autosos_flutter/pages/home/widgets/number_block.dart'; @@ -15,7 +13,6 @@ class HomePage extends StatefulWidget { class _HomePageState extends State { bool enabled = false; - late AMapController _mapController; @override Widget build(BuildContext context) { @@ -227,59 +224,4 @@ class _HomePageState extends State { ), ); } - - Widget buildBottomWidget() { - AMapWidget map = AMapWidget( - ///必须正确设置的合规隐私声明,否则SDK不会工作,会造成地图白屏等问题。 - privacyStatement: ConstConfig.amapPrivacyStatement, - apiKey: ConstConfig.amapApiKeys, - mapType: MapType.navi, - scaleEnabled: false, - myLocationStyleOptions: MyLocationStyleOptions( - true, - circleFillColor: Colors.lightBlue, - circleStrokeColor: Colors.blue, - circleStrokeWidth: 1, - ), - onMapCreated: (AMapController controller) { - setState(() { - _mapController = controller; - getApprovalNumber(); - }); - }, - ); - return Expanded( - child: Stack( - children: [ - Container( - margin: const EdgeInsets.only(top: 10, bottom: 10), - decoration: const BoxDecoration( - color: Colors.white, - borderRadius: BorderRadius.all(Radius.circular(5))), - child: map, - ), - Positioned( - bottom: 20, - right: 20, - child: Image.asset( - "images/3.0x/current_location.png", - width: 30, - height: 30, - ), - ) - ], - )); - } - - ///获取审图号 - void getApprovalNumber() async { - //普通地图审图号 - String? mapContentApprovalNumber = - await _mapController.getMapContentApprovalNumber(); - //卫星地图审图号 - String? satelliteImageApprovalNumber = - await _mapController.getSatelliteImageApprovalNumber(); - print('地图审图号(普通地图): $mapContentApprovalNumber'); - print('地图审图号(卫星地图): $satelliteImageApprovalNumber'); - } } diff --git a/lib/pages/home/widgets/map_widget.dart b/lib/pages/home/widgets/map_widget.dart index 24cdf88..399bbe5 100644 --- a/lib/pages/home/widgets/map_widget.dart +++ b/lib/pages/home/widgets/map_widget.dart @@ -1,6 +1,12 @@ +import 'dart:async'; +import 'dart:io'; + +import 'package:amap_flutter_location/amap_flutter_location.dart'; +import 'package:amap_flutter_location/amap_location_option.dart'; import 'package:amap_flutter_map/amap_flutter_map.dart'; import 'package:autosos_flutter/const_config.dart'; import 'package:flutter/material.dart'; +import 'package:permission_handler/permission_handler.dart'; @@ -13,6 +19,69 @@ class MapWidget extends StatefulWidget { class _MapWidgetState extends State { late AMapController _mapController; + late Map _locationResult; + + @override + void initState() { + super.initState(); + /// 设置是否已经包含高德隐私政策并弹窗展示显示用户查看,如果未包含或者没有弹窗展示,高德定位SDK将不会工作 + /// + /// 高德SDK合规使用方案请参考官网地址:https://lbs.amap.com/news/sdkhgsy + /// 必须保证在调用定位功能之前调用, 建议首次启动App时弹出《隐私政策》并取得用户同意 + /// + /// 高德SDK合规使用方案请参考官网地址:https://lbs.amap.com/news/sdkhgsy + /// + /// [hasContains] 隐私声明中是否包含高德隐私政策说明 + /// + /// [hasShow] 隐私权政策是否弹窗展示告知用户 + AMapFlutterLocation.updatePrivacyShow(true, true); + + /// 设置是否已经取得用户同意,如果未取得用户同意,高德定位SDK将不会工作 + /// + /// 高德SDK合规使用方案请参考官网地址:https://lbs.amap.com/news/sdkhgsy + /// + /// 必须保证在调用定位功能之前调用, 建议首次启动App时弹出《隐私政策》并取得用户同意 + /// + /// [hasAgree] 隐私权政策是否已经取得用户同意 + AMapFlutterLocation.updatePrivacyAgree(true); + + /// 动态申请定位权限 + requestPermission(); + + ///设置Android和iOS的apiKey
+ /// + /// 定位Flutter插件提供了单独的设置ApiKey的接口, + /// 使用接口的优先级高于通过Native配置ApiKey的优先级(通过Api接口配置后,通过Native配置文件设置的key将不生效), + /// 使用时可根据实际情况决定使用哪种方式 + /// + ///key的申请请参考高德开放平台官网说明
+ /// + ///Android: https://lbs.amap.com/api/android-location-sdk/guide/create-project/get-key + /// + ///iOS: https://lbs.amap.com/api/ios-location-sdk/guide/create-project/get-key + AMapFlutterLocation.setApiKey( + ConstConfig.androidKey, ConstConfig.iosKey); + + + ///iOS 获取native精度类型 + if (Platform.isIOS) { + requestAccuracyAuthorization(); + } + + ///注册定位结果监听 + _locationListener = _locationPlugin + .onLocationChanged() + .listen((Map result) { + setState(() { + _locationResult = result; + }); + print("locationRest=$_locationResult"); + }); + } + + late StreamSubscription> _locationListener; + + final AMapFlutterLocation _locationPlugin = new AMapFlutterLocation(); @override Widget build(BuildContext context) { AMapWidget map = AMapWidget( @@ -57,6 +126,7 @@ class _MapWidgetState extends State { )); } + ///获取审图号 void getApprovalNumber() async { //普通地图审图号 @@ -68,4 +138,49 @@ class _MapWidgetState extends State { print('地图审图号(普通地图): $mapContentApprovalNumber'); print('地图审图号(卫星地图): $satelliteImageApprovalNumber'); } + + void requestPermission() async { +// 申请权限 + bool hasLocationPermission = await requestLocationPermission(); + if (hasLocationPermission) { + print("定位权限申请通过"); + } else { + print("定位权限申请不通过"); + } + } + + /// 申请定位权限 + /// 授予定位权限返回true, 否则返回false + Future requestLocationPermission() async { + //获取当前的权限 + var status = await Permission.locationAlways.status; + if (status == PermissionStatus.granted) { + //已经授权 + return true; + } else { + //未授权则发起一次申请 + status = await Permission.locationAlways.request(); + print("status=$status"); + if (status == PermissionStatus.granted) { + return true; + } else { + return false; + } + } + } + + ///获取iOS native的accuracyAuthorization类型 + void requestAccuracyAuthorization() async { + AMapAccuracyAuthorization currentAccuracyAuthorization = + await _locationPlugin.getSystemAccuracyAuthorization(); + if (currentAccuracyAuthorization == + AMapAccuracyAuthorization.AMapAccuracyAuthorizationFullAccuracy) { + print("精确定位类型"); + } else if (currentAccuracyAuthorization == + AMapAccuracyAuthorization.AMapAccuracyAuthorizationReducedAccuracy) { + print("模糊定位类型"); + } else { + print("未知定位类型"); + } + } } diff --git a/lib/pages/login/login_page.dart b/lib/pages/login/login_page.dart index ac25726..a65d345 100644 --- a/lib/pages/login/login_page.dart +++ b/lib/pages/login/login_page.dart @@ -19,6 +19,8 @@ class _LoginPageState extends State { SPUtil spUtil = SPUtil(); @override Widget build(BuildContext context) { + _unameController.text = "14725803690"; + _pwdController.text = "123456"; return Scaffold( appBar: AppBar(), body: Column( @@ -70,6 +72,7 @@ class _LoginPageState extends State { children: [ Expanded( child: TextField( + obscureText: true, controller: _pwdController, decoration: const InputDecoration(hintText: "请输入密码"), )) diff --git a/pubspec.lock b/pubspec.lock index fba33f5..2e607a0 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -312,6 +312,54 @@ packages: url: "https://pub.flutter-io.cn" source: hosted version: "2.2.1" + permission_handler: + dependency: "direct main" + description: + name: permission_handler + sha256: "74e962b7fad7ff75959161bb2c0ad8fe7f2568ee82621c9c2660b751146bfe44" + url: "https://pub.flutter-io.cn" + source: hosted + version: "11.3.0" + permission_handler_android: + dependency: transitive + description: + name: permission_handler_android + sha256: "1acac6bae58144b442f11e66621c062aead9c99841093c38f5bcdcc24c1c3474" + url: "https://pub.flutter-io.cn" + source: hosted + version: "12.0.5" + permission_handler_apple: + dependency: transitive + description: + name: permission_handler_apple + sha256: bdafc6db74253abb63907f4e357302e6bb786ab41465e8635f362ee71fd8707b + url: "https://pub.flutter-io.cn" + source: hosted + version: "9.4.0" + permission_handler_html: + dependency: transitive + description: + name: permission_handler_html + sha256: "54bf176b90f6eddd4ece307e2c06cf977fb3973719c35a93b85cc7093eb6070d" + url: "https://pub.flutter-io.cn" + source: hosted + version: "0.1.1" + permission_handler_platform_interface: + dependency: transitive + description: + name: permission_handler_platform_interface + sha256: "23dfba8447c076ab5be3dee9ceb66aad345c4a648f0cac292c77b1eb0e800b78" + url: "https://pub.flutter-io.cn" + source: hosted + version: "4.2.0" + permission_handler_windows: + dependency: transitive + description: + name: permission_handler_windows + sha256: "1a790728016f79a41216d88672dbc5df30e686e811ad4e698bfc51f76ad91f1e" + url: "https://pub.flutter-io.cn" + source: hosted + version: "0.2.1" platform: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 7c1f9b0..fd3d49f 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -40,6 +40,7 @@ dependencies: uuid: ^4.3.3 device_info_plus: ^9.1.2 getuiflut: ^0.2.25 + permission_handler: ^11.3.0 # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons.