效果
插件安装以及环境配置
是使用amap_all_fluttify插件来完成的高德地图的集成
根据官方的说明,这个插件集成了amap_search_fluttify,amap_location_fluttify,amap_map_fluttify.这些插件。
安装amap_all_fluttify 插件后,还需要根据每个插件进行配置。
例如amap_search_fluttify
按照官方文档进行配置即可。
之后就可以尝试进行运行,但是这时会出现一个报错:
Error:The number of method references in a .dex file cannot exceed 64K.
我才用的解决办法才用的是 https://blog.csdn.net/nakajimafn/article/details/82668793 这个链接对应的第二个方法。
代码编写
文档结构
main.dart
import 'package:flutter/material.dart';
import 'package:amap_all_fluttify/amap_all_fluttify.dart';
import 'components/map_choicePoint.dart';
Future main() async {
runApp(MyApp());
//地图基本功能-注册高德key
await AmapService.init(androidKey: '');
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'AMAP_Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'AMAP_Demo'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(child: MapChoicePoint((point) {
debugPrint(point.toString());
})),
);
}
}
map_choicePoint.dart
import 'package:flutter/material.dart';
import 'package:amap_all_fluttify/amap_all_fluttify.dart';
import 'package:decorated_flutter/decorated_flutter.dart';
import 'package:flutter/services.dart';
import 'package:permission_handler/permission_handler.dart';
/**
* 地图选择点控件
*/
class MapChoicePoint extends StatefulWidget {
/**
* 选择点后回调事件
*/
final Function onChoicePoint;
MapChoicePoint(this.onChoicePoint);
@override
_MapChoicePointState createState() => _MapChoicePointState();
}
class _MapChoicePointState extends State
with SingleTickerProviderStateMixin {
//----属性----
//地图控制器
AmapController _amapController;
//选择的点
Marker _markerSelect;
//搜索出来之后选择的点
Marker _markerSeached;
//所在城市
String city;
//搜索框文字控制器
TextEditingController _serachController;
//自定义marker点图标图片路径
Uri _imgUri = Uri.parse('images/position.png');
//----方法----
/**
* 获取权限
*/
Future _requestPermission() async {
final permissions = await PermissionHandler()
.requestPermissions([PermissionGroup.location]);
if (permissions[PermissionGroup.location] == PermissionStatus.granted) {
return true;
} else {
toast('需要定位权限!');
return false;
}
}
/**
* 根据搜索条件选出想要的点
*/
Future _openModalBottomSheet() async {
//收起键盘
FocusScope.of(context).requestFocus(FocusNode());
//根据关键字及城市进行搜索
final poiList = await AmapSearch.searchKeyword(
_serachController.text,
city: city,
);
List