准备工作
高德开放平台地址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 {
storeFile file('androidkeystore.jks')
keyAlias "key0"
storePassword "20010101"
keyPassword "20010101"
}
debug {
storeFile file('androidkeystore.jks')
keyAlias "key0"
storePassword "20010101"
keyPassword "20010101"
}
}
buildTypes {
release {
signingConfig signingConfigs.release
}
debug {
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
<meta-data android:name="com.amap.api.v2.apikey" android:value="您的Key">
</meta-data>
- application标签中声明service组件
<service android:name="com.amap.api.location.APSService"></service>
- 在AndroidManifest.xml中配置权限:
<!--允许访问网络,必选权限-->
<uses-permission android:name="android.permission.INTERNET" />
<!--允许获取精确位置,精准定位必选-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!--允许获取粗略位置,粗略定位必选-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!--允许获取设备和运营商信息,用于问题排查和网络定位(无gps情况下的定位),若需网络定位功能则必选-->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<!--允许获取网络状态,用于网络定位(无gps情况下的定位),若需网络定位功能则必选-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!--允许获取wifi网络信息,用于网络定位(无gps情况下的定位),若需网络定位功能则必选-->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!--允许获取wifi状态改变,用于网络定位(无gps情况下的定位),若需网络定位功能则必选-->
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<!--后台获取位置信息,若需后台定位则必选-->
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<!--用于申请调用A-GPS模块,卫星定位加速-->
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<!--允许写设备缓存,用于问题排查-->
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<!--允许写入扩展存储,用于写入缓存定位数据-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!--允许读设备等信息,用于问题排查-->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
获取定位
import 'package:flutter/material.dart';
import 'dart:async';
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<LocationPage> {
String _latitude = "";
String _longitude = "";
late StreamSubscription<Map<String, Object>> _locationListener;
AMapFlutterLocation _locationPlugin = new AMapFlutterLocation();
@override
void initState() {
super.initState();
requestPermission();
AMapFlutterLocation.updatePrivacyAgree(true);
AMapFlutterLocation.updatePrivacyShow(true, true);
AMapFlutterLocation.setApiKey(
"4f72d5a172de996b05aace4eb52bd7cd", "ios ApiKey");
_locationListener = _locationPlugin
.onLocationChanged()
.listen((Map<String, Object> result) {
setState(() {
print(result);
print("-----");
print(result is Map);
_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";
locationOption.locationInterval = 2000;
locationOption.locationMode = AMapLocationMode.Hight_Accuracy;
locationOption.distanceFilter = -1;
locationOption.desiredAccuracy = DesiredAccuracy.Best;
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("定位权限申请不通过");
}
}
Future<bool> 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: [
Text("纬度:${this._latitude}"),
Text("经度:${this._longitude}"),
SizedBox(height: 20),
ElevatedButton(
child: Text('开始定位'),
onPressed: () {
this._startLocation();
},
),
],
),
),
);
}
}
|