Flutter集成高德地图定位组件
2019-11-16 本文已影响0人
陌北v1
插件地址github:
https://github.com/fluttify-project/amap_location_fluttify
flutter pub:
https://pub.flutter-io.cn/packages/amap_location_fluttify
Flutter 签名方法(如果没有生成过签名文件,参考此文章)
https://www.jianshu.com/p/248eeb5d6535
Android Studio配置:
1.配置 android\app\src\main\AndroidManifest.xml文件
在AndroidManifest.xml的application
标签中配置Key(这一步不要搞错了。是application
标签中配置Key):
<meta-data
android:name="com.amap.api.v2.apikey"
android:value="您的Key"/>
在AndroidManifest.xml中配置权限:
//地图包、搜索包需要的基础权限
<!--允许程序打开网络套接字-->
<uses-permission android:name="android.permission.INTERNET" />
<!--允许程序设置内置sd卡的写权限-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!--允许程序获取网络状态-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!--允许程序访问WiFi网络信息-->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!--允许程序读写手机状态和身份-->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<!--允许程序访问CellID或WiFi热点来获取粗略的位置-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
开始安装:
dependencies:
amap_location_fluttify: ^0.2.7+d2eb645
导入:
import 'package:amap_location_fluttify/amap_location_fluttify.dart';
使用:
await AmapCore.init('ios key');//IOS需要,安卓不需要
if (await requestPermission()) {
await AmapLocation.startLocation(
once: true,
locationChanged: (location) {
_location = location;
setState(() {});
},
);
}
例子:
import 'package:amap_core_fluttify/amap_core_fluttify.dart';
import 'package:amap_location_fluttify/amap_location_fluttify.dart';
import 'package:decorated_flutter/decorated_flutter.dart';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
void main() async {
await AmapCore.init('f6422eadda731fb0d9ffb3260a5cf899');
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> with AmapLocationDisposeMixin {
Location _location;
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Plugin example app')),
body: DecoratedColumn(
padding: EdgeInsets.symmetric(horizontal: kSpaceLarge),
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
child: Text('获取单次定位'),
onPressed: () async {
if (await requestPermission()) {
AmapLocation.startLocation(
once: true,
locationChanged: (location) {
_location = location;
setState(() {});
},
);
}
},
),
RaisedButton(
child: Text('获取连续定位'),
onPressed: () async {
if (await requestPermission()) {
AmapLocation.startLocation(
once: false,
locationChanged: (location) {
_location = location;
setState(() {});
},
);
}
},
),
RaisedButton(
child: Text('停止定位'),
onPressed: () async {
if (await requestPermission()) {
await AmapLocation.stopLocation();
}
},
),
if (_location != null)
FutureBuilder<String>(
initialData: '',
future: _location.address,
builder: (_, ss) =>
Center(child: Text('address: ${ss.data}' ?? '')),
),
if (_location != null)
FutureBuilder<String>(
initialData: '',
future: _location.country,
builder: (_, ss) =>
Center(child: Text('country: ${ss.data}' ?? '')),
),
if (_location != null)
FutureBuilder<String>(
initialData: '',
future: _location.province,
builder: (_, ss) =>
Center(child: Text('province: ${ss.data}' ?? '')),
),
if (_location != null)
FutureBuilder<String>(
initialData: '',
future: _location.city,
builder: (_, ss) =>
Center(child: Text('city: ${ss.data}' ?? '')),
),
if (_location != null)
FutureBuilder<String>(
initialData: '',
future: _location.district,
builder: (_, ss) =>
Center(child: Text('district: ${ss.data}' ?? '')),
),
if (_location != null)
FutureBuilder<String>(
initialData: '',
future: _location.poiName,
builder: (_, ss) =>
Center(child: Text('poiName: ${ss.data}' ?? '')),
),
if (_location != null)
FutureBuilder<String>(
initialData: '',
future: _location.street,
builder: (_, ss) =>
Center(child: Text('street: ${ss.data}' ?? '')),
),
if (_location != null)
FutureBuilder<String>(
initialData: '',
future: _location.aoiName,
builder: (_, ss) =>
Center(child: Text('aoiName: ${ss.data}' ?? '')),
),
if (_location != null)
FutureBuilder<double>(
initialData: 0.0,
future: _location.latitude,
builder: (_, ss) =>
Center(child: Text('latitude: ${ss.data}' ?? '')),
),
if (_location != null)
FutureBuilder<double>(
initialData: 0.0,
future: _location.longitude,
builder: (_, ss) =>
Center(child: Text('longitude: ${ss.data}' ?? '')),
),
if (_location != null)
FutureBuilder<double>(
initialData: 0.0,
future: _location.altitude,
builder: (_, ss) =>
Center(child: Text('altitude: ${ss.data}' ?? '')),
),
],
),
),
);
}
}
Future<bool> requestPermission() async {
final permissions =
await PermissionHandler().requestPermissions([PermissionGroup.location]);
if (permissions[PermissionGroup.location] == PermissionStatus.granted) {
return true;
} else {
toast('需要定位权限!');
return false;
}
}