欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

flutter集成高德地图获取位置

时间:2023-08-09
flutter集成高德地图获取位置

准备工作

在创建安卓应用获取SHA1 获取当前位置

添加依赖文件配置

build.gradle文件配置AndroidManifest.xml配置 获取定位 准备工作

高德开放平台地址https://lbs.amap.com/tools/picker

在创建安卓应用 在控制台我的应用中创建应用
点击添加

由上图可以看出我们还需要获取安全码SHA1才能添加
添加方法高德官方也已经给出
https://lbs.amap.com/faq/android/map-sdk/create-project/43112 获取SHA1

这里我们采用的是使用 keytool(jdk自带工具)获取SHA1

在控制台输入cd .android定位到.android目录下继续在控制台输入命令:调试版本使用 debug.keystore,命令为:keytool -list -v -keystore debug.keystore 发布版本使用 apk 对应的 keystore,命令为:keytool -list -v -keystore apk的keystore按照要求输入密钥库口令,注意这里输入后是没有显示的
选择sha1复制即可(注意调试版安全码和发布版安全码我们可以都写成一样的)

准备工作完成后创建的应用如图

获取当前位置 添加依赖

高德定位Flutter插件地址https://pub.dev/packages/amap_flutter_location
添加依赖

dependencies: amap_flutter_location: ^3.0.0

导包

import 'package:amap_flutter_location/amap_flutter_location.dart';

权限检测插件地址https://pub.dev/packages/permission_handler

添加依赖

dependencies: permission_handler: ^8.3.0

导包

import 'package:permission_handler/permission_handler.dart';

文件配置

高德官方给出一定的指引https://developer.amap.com/api/flutter/gettingstarted

build.gradle文件配置

修改你的android/app/build.gradle文件配置签名文件以及配置 implementation

signingConfigs { release { //发布版本的签名配置 //还要先将我们使用的androidkeystore.jks'导入到flutter_apponeandroid下 storeFile file('androidkeystore.jks') keyAlias "key0" storePassword "20010101" keyPassword "20010101" } debug { //调试版本的签名配置 storeFile file('androidkeystore.jks') keyAlias "key0" storePassword "20010101" keyPassword "20010101" } } buildTypes { release { // TODO: Add your own signing config for the release build. // Signing with the debug keys for now, so `flutter run --release` works. signingConfig signingConfigs.release } debug { // TODO: Add your own signing config for the release build、 // Signing with the debug keys for now, so `flutter run --release` works、 signingConfig signingConfigs.debug } }

使用高德SDK之前配置可以参考高德官方文档https://developer.amap.com/api/android-location-sdk/guide/create-project/android-studio-create-project

dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation "com.amap.api:location:latest.integration"}

AndroidManifest.xml配置

android/app/src/main/AndroidManifest.xml下的配置

主要用于配置高德Key和申请权限:

在AndroidManifest.xml的application标签中配置Key:

application标签中配置Key

application标签中声明service组件

在AndroidManifest.xml中配置权限:

获取定位

import 'package:flutter/material.dart';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:permission_handler/permission_handler.dart';class LocationPage extends StatefulWidget { LocationPage({Key? key}) : super(key: key); _LocationPageState createState() => _LocationPageState();}class _LocationPageState extends State { //获取数据 // Map _locationResult; String _latitude = ""; //纬度 String _longitude = ""; //经度 //监听定位 late StreamSubscription> _locationListener; //实例化插件 AMapFlutterLocation _locationPlugin = new AMapFlutterLocation(); @override void initState() { // TODO: implement initState super.initState(); /// 动态申请定位权限 requestPermission(); AMapFlutterLocation.updatePrivacyAgree(true); AMapFlutterLocation.updatePrivacyShow(true, true); AMapFlutterLocation.setApiKey( "4f72d5a172de996b05aace4eb52bd7cd", "ios ApiKey"); ///注册定位结果监听 _locationListener = _locationPlugin .onLocationChanged() .listen((Map result) { setState(() { print(result); print("-----"); print(result is Map); // _locationResult = result; _latitude = result["latitude"].toString(); _longitude = result["longitude"].toString(); }); }); } @override void dispose() { super.dispose(); ///移除定位监听 if (null != _locationListener) { _locationListener.cancel(); } ///销毁定位 if (null != _locationPlugin) { _locationPlugin.destroy(); } } ///设置定位参数 void _setLocationOption() { if (null != _locationPlugin) { AMapLocationOption locationOption = new AMapLocationOption(); ///是否单次定位 locationOption.onceLocation = false; ///是否需要返回逆地理信息 locationOption.needAddress = true; ///逆地理信息的语言类型 locationOption.geoLanguage = GeoLanguage.DEFAULT; locationOption.desiredLocationAccuracyAuthorizationMode = AMapLocationAccuracyAuthorizationMode.ReduceAccuracy; locationOption.fullAccuracyPurposeKey = "AMapLocationScene"; ///设置Android端连续定位的定位间隔 locationOption.locationInterval = 2000; ///设置Android端的定位模式
///可选值:
///[AMapLocationMode.Battery_Saving] ///[AMapLocationMode.Device_Sensors] ///[AMapLocationMode.Hight_Accuracy] locationOption.locationMode = AMapLocationMode.Hight_Accuracy; ///设置iOS端的定位最小更新距离
locationOption.distanceFilter = -1; ///设置iOS端期望的定位精度 /// 可选值:
/// [DesiredAccuracy.Best] 最高精度 /// [DesiredAccuracy.BestForNavigation] 适用于导航场景的高精度 /// [DesiredAccuracy.NearestTenMeters] 10米 /// [DesiredAccuracy.Kilometer] 1000米 /// [DesiredAccuracy.ThreeKilometers] 3000米 locationOption.desiredAccuracy = DesiredAccuracy.Best; ///设置iOS端是否允许系统暂停定位 locationOption.pausesLocationUpdatesAutomatically = false; ///将定位参数设置给定位插件 _locationPlugin.setLocationOption(locationOption); } } ///开始定位 void _startLocation() { if (null != _locationPlugin) { ///开始定位之前设置定位参数 _setLocationOption(); _locationPlugin.startLocation(); } } /// 动态申请定位权限 void requestPermission() async { // 申请权限 bool hasLocationPermission = await requestLocationPermission(); if (hasLocationPermission) { print("定位权限申请通过"); } else { print("定位权限申请不通过"); } } /// 申请定位权限 授予定位权限返回true, 否则返回false Future requestLocationPermission() async { //获取当前的权限 var status = await Permission.location.status; if (status == PermissionStatus.granted) { //已经授权 return true; } else { //未授权则发起一次申请 status = await Permission.location.request(); if (status == PermissionStatus.granted) { return true; } else { return false; } } } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text("地理定位演示"), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ // latitude: 36.570091461155336, longitude: 109.5080830206976 // Text("纬度:${this._latitude}"), Text("经度:${this._longitude}"), SizedBox(height: 20), ElevatedButton( child: Text('开始定位'), onPressed: () { this._startLocation(); }, ), ], ), ), ); }}


Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。