本文主要是介绍使用flutter的amap_location_flutter进行高德定位,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
按照高德官网走一直拿不到城市信息,改的乱七八糟,反正现在能拿到了,主要代码还是官网给的例子,从github上下载并运行了一下,有把代码粘贴到自己的项目当中,进行了修改,之前一直拿不到city等的信息
配置过程不简单,而且坑很多,自求多福吧
https://lbs.amap.com/api/flutter/summary
https://github.com/amap-demo/amap-location-flutter
import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:amap_location_flutter_plugin/amap_location_flutter_plugin.dart';
import 'package:amap_location_flutter_plugin/amap_location_option.dart';String _locationText;
class AmapPage extends StatefulWidget {@override_AmapPageState createState() => new _AmapPageState();
}class _AmapPageState extends State<AmapPage> {Map<String, Object> _locationResult;StreamSubscription<Map<String, Object>> _locationListener;// AMapFlutterLocation _locationPlugin = new AMapFlutterLocation();AmapLocationFlutterPlugin _locationPlugin = new AmapLocationFlutterPlugin();@overridevoid initState() {super.initState();// 这个api是我在网上找的,必须用自己的,我的代码里也是我自己去申请的,ios的可以先不申请,但是要写,我的ios的key是用下面的这个AmapLocationFlutterPlugin.setApiKey('b9f1308d2ad232648d3a5714eacf3a6d', 'b9f1308d2ad232648d3a5714eacf3a6d');/// 动态申请定位权限requestPermission();///设置Android和iOS的apiKey<br>////// 定位Flutter插件提供了单独的设置ApiKey的接口,/// 使用接口的优先级高于通过Native配置ApiKey的优先级(通过Api接口配置后,通过Native配置文件设置的key将不生效),/// 使用时可根据实际情况决定使用哪种方式//////key的申请请参考高德开放平台官网说明<br>//////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(// "anroid ApiKey", "ios ApiKey");///iOS 获取native精度类型if (Platform.isIOS) {requestAccuracyAuthorization();}///注册定位结果监听_locationListener = _locationPlugin.onLocationChanged().listen((Map<String, Object> result) {setState(() {_locationResult = result;print('定位结果${result}');_locationText = ' ' +_locationResult['province'] +_locationResult['city'] +_locationResult['district'] +_locationResult['street'];});print('详细信息${_locationText}');});}@overridevoid 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.ZH;locationOption.desiredLocationAccuracyAuthorizationMode =AMapLocationAccuracyAuthorizationMode.ReduceAccuracy;locationOption.fullAccuracyPurposeKey = "AMapLocationScene";///设置Android端连续定位的定位间隔locationOption.locationInterval = 2000;///设置Android端的定位模式<br>///可选值:<br>///<li>[AMapLocationMode.Battery_Saving]</li>///<li>[AMapLocationMode.Device_Sensors]</li>///<li>[AMapLocationMode.Hight_Accuracy]</li>locationOption.locationMode = AMapLocationMode.Hight_Accuracy;///设置iOS端的定位最小更新距离<br>locationOption.distanceFilter = -1;///设置iOS端期望的定位精度/// 可选值:<br>/// <li>[DesiredAccuracy.Best] 最高精度</li>/// <li>[DesiredAccuracy.BestForNavigation] 适用于导航场景的高精度 </li>/// <li>[DesiredAccuracy.NearestTenMeters] 10米 </li>/// <li>[DesiredAccuracy.Kilometer] 1000米</li>/// <li>[DesiredAccuracy.ThreeKilometers] 3000米</li>locationOption.desiredAccuracy = DesiredAccuracy.Best;///设置iOS端是否允许系统暂停定位locationOption.pausesLocationUpdatesAutomatically = false;///将定位参数设置给定位插件_locationPlugin.setLocationOption(locationOption);}}///开始定位void _startLocation() {if (null != _locationPlugin) {///开始定位之前设置定位参数_setLocationOption();_locationPlugin.startLocation();}}///停止定位void _stopLocation() {if (null != _locationPlugin) {_locationPlugin.stopLocation();}}Container _createButtonContainer() {return new Container(alignment: Alignment.center,child: new Row(mainAxisSize: MainAxisSize.min,crossAxisAlignment: CrossAxisAlignment.center,children: <Widget>[new RaisedButton(onPressed: _startLocation,child: new Text('开始定位'),color: Colors.blue,textColor: Colors.white,),new Container(width: 20.0),new RaisedButton(onPressed: _stopLocation,child: new Text('停止定位'),color: Colors.blue,textColor: Colors.white,)],));}Widget _resultWidget(key, value) {return new Container(child: new Row(mainAxisSize: MainAxisSize.min,crossAxisAlignment: CrossAxisAlignment.center,children: <Widget>[new Container(alignment: Alignment.centerRight,width: 100.0,child: new Text('$key :'),),new Container(width: 5.0),new Flexible(child: new Text('$value', softWrap: true)),],),);}@overrideWidget build(BuildContext context) {List<Widget> widgets = new List();widgets.add(_createButtonContainer());if (_locationResult != null) {_locationResult.forEach((key, value) {widgets.add(_resultWidget(key, value));});}return new MaterialApp(home: new Scaffold(appBar: new AppBar(title: new Text('AMap Location plugin example app'),),body: new Column(crossAxisAlignment: CrossAxisAlignment.start,mainAxisSize: MainAxisSize.min,children: widgets,),));}///获取iOS native的accuracyAuthorization类型void requestAccuracyAuthorization() async {AMapAccuracyAuthorization currentAccuracyAuthorization =await _locationPlugin.getSystemAccuracyAuthorization();if (currentAccuracyAuthorization ==AMapAccuracyAuthorization.AMapAccuracyAuthorizationFullAccuracy) {print("精确定位类型");} else if (currentAccuracyAuthorization ==AMapAccuracyAuthorization.AMapAccuracyAuthorizationReducedAccuracy) {print("模糊定位类型");} else {print("未知定位类型");}}/// 动态申请定位权限void requestPermission() async {// 申请权限bool hasLocationPermission = await requestLocationPermission();if (hasLocationPermission) {print("定位权限申请通过");} else {print("定位权限申请不通过");}}/// 申请定位权限/// 授予定位权限返回true, 否则返回falseFuture<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;}}}
}
这篇关于使用flutter的amap_location_flutter进行高德定位的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!