组件都在这--->pub.dev/
amap_location获取地理位置
-
首先要在pubspec.yaml中安装
dependencies: amap_location: ^0.2.0 -
在dart文件中使用
import 'package:amap_location/amap_location.dart';
在使用高德地图定位之前要进入高德api获取Key
过程:登录->应用管理->我的应用->右上角创建新的应用->点击“添加key”按钮->依次输入名称,服务平台为Android平台,安全码SHA1,Package。 获取SHA1:
打开黑窗口,输入:cd .android
进入.android之后输入:keytool -list -v -keystore debug.keystore
然后找到SHA1
Package:在android--app--src--main下的AndroidManifest.xml中找到包的名字 提交之后就可以获取到Key了
- 接着我们的配置找到目录android--app--build.gradle,添加以下代码
android {
.... 你的代码
defaultConfig {
.....
manifestPlaceholders = [
AMAP_KEY : "aa9f0cf8574400f2af0078392c556e25", /// 高德地图key
]
}
...你的代码
dependencies {
/// 注意这里需要在主项目增加一条依赖,否则可能发生编译不通过的情况
implementation 'com.amap.api:location:latest.integration'
...你的代码
}
要把刚刚获取到Key的值换一下
class _LocationPageState extends State<LocationPage> {
double _longitude = 0;
double _latitude = 0;
@override
void initState() {
super.initState();
this._getLocation(); //打开这个页面的时候就触发这个方法
}
_getLocation() async{
//启动一下
await AMapLocationClient.startup(new AMapLocationOption( desiredAccuracy:CLLocationAccuracy.kCLLocationAccuracyHundredMeters ));
//获取地理位置
var result = await AMapLocationClient.getLocation(true);
print("经度:${result.longitude}");
print("纬度:${result.latitude}");
setState(() {
this._longitude=result.longitude;
this._latitude=result.latitude;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("地理位置演示"),
),
body: Column(
children: <Widget>[
Text("经度:${this._longitude}"),
Text("纬度:${this._latitude}"),
],
),
);
}
}