flutter 高德地图api使用 app内集成定位 地图搜索功能
Posted YunusQ
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了flutter 高德地图api使用 app内集成定位 地图搜索功能相关的知识,希望对你有一定的参考价值。
flutter 高德地图api使用 app内集成定位 地图搜索功能
amap_all_fluttify插件(现在不知道咋的没有了)
这个插件就是集成了amap_map_fluttify amap_search_fluttify amap_location_fluttify三个插件 分别在pubspec里导入就行
main.dart里先注册 获取andriod和ios的相关key
///地图基本功能-注册高德key
await AmapService.init(androidKey: 'xxxxxxx', iosKey: "yyyyyyy");
下面是关键代码
这里是点击选择后的跳转
(ios14有地图定位bug 一旦要定位自己的位置就会报错 所以ios14取消定位自己 只有地图显示 ios13及以下都没有问题 求助高德官方也无果 因为此插件非官方插件)
/*等待返回数据是异步操作*/
void chooseActAddress() async
// final result2 = await Navigator.push(//等待
// context,MaterialPageRoute(builder: (context)=> SelectLocationFromMapPage())
// );
var add = await Navigator.pushNamed(context, '/SelectLocation');
print(add);
Map tmp = add;
provinceName = tmp['provinceName'];
cityName = tmp['cityName'];
provinceName = provinceName.substring(0, provinceName.length - 1);
cityName = cityName.substring(0, cityName.length - 1);
district = tmp['district'];
addressTitle = tmp['title'];
latitude = tmp['latitude'];
longitude = tmp['longitude'];
print("addressTitle" + addressTitle);
if (mounted)
setState(()
textControllerAddress.text = tmp['title'];
);
// print(district);
print(provinceName + cityName);
这里是想要选择地址后的跳转代码
import 'package:amap_map_fluttify/amap_map_fluttify.dart';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:amap_search_fluttify/amap_search_fluttify.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:amap_location_fluttify/amap_location_fluttify.dart';
import 'package:youdianle_app_flutter/views/form/pageloading.dart';
class SelectLocationFromMapPage extends StatefulWidget
@override
_SelectLocationFromMapPageState createState() => _SelectLocationFromMapPageState();
class _SelectLocationFromMapPageState extends State<SelectLocationFromMapPage>
AmapController _controller;
List<Poi> poiList;
static List<PoiModel> list = new List();
int showType=0;
// static List<PoiModel> searchlist = new List();
PoiModel poiModel;
String keyword = "";
String address = "";
String nowProvinceName = "";
String nowCityName = "";
String nowDistrictName = "";
String nowLatitude = "";
String nowLongitude = "";
bool isloading = true;
// var _location;
@override
void initState()
super.initState();
checkPersmission();
void checkPersmission() async
// 申请权限
// Map<PermissionGroup, PermissionStatus> permissions =
await PermissionHandler().requestPermissions([PermissionGroup.location]);
// 申请结果
PermissionStatus permission = await PermissionHandler().checkPermissionStatus(PermissionGroup.location);
if (permission == PermissionStatus.granted)
//_getLocation();
else
// bool isOpened =
await PermissionHandler().openAppSettings();//打开应用设置
// static Future<bool> requestPermission() async
// final permissions = await PermissionHandler().requestPermissions([PermissionGroup.location]);
//
// if (permissions[PermissionGroup.location] == PermissionStatus.granted)
// return true;
// else
// return false;
//
//
@override
Widget build(BuildContext context)
return Scaffold(
resizeToAvoidBottomPadding: false, //防止底部布局被顶起
appBar: AppBar(
backgroundColor: Color(0xff5653ff),
title: Text(
'选择位置信息',
),
elevation: 0.0,
centerTitle: true,
),
body: Column(
children: <Widget>[
Theme(
data: new ThemeData(primaryColor: Color(0xffFE6004), hintColor: Color(0xffFE6004)),
child: Container(
color: Color(0xffFE6004),
padding: EdgeInsets.all(3),
child: Container(
height: 33,
margin: EdgeInsets.only(left: 5, right: 5, bottom: 5),
child: Row(children: <Widget>[
if(showType==1)
Expanded(child: IconButton(icon: Icon(Icons.arrow_back_ios,color: Colors.white,size: 20,),onPressed: ()
print("back prev");
setState(()
showType=0;
);
,)),
Expanded(child: TextField(
style: TextStyle(fontSize: 16, letterSpacing: 1.0),
controller: TextEditingController.fromValue(TextEditingValue(
// 设置内容
text: keyword,
selection: TextSelection.fromPosition(
TextPosition(affinity: TextAffinity.downstream, offset: keyword?.length ?? 0)),
// 保持光标在最后
)),
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(40)),
),
hintText: '输入关键字',
hintStyle: TextStyle(color: Color(0xFFBEBEBE), fontSize: 14),
contentPadding: const EdgeInsets.symmetric(vertical: 8, horizontal: 1),
prefixIcon: Icon(
Icons.search,
color: Colors.grey,
size: 20,
),
suffixIcon: IconButton(
icon: Icon(
Icons.clear,
color: Colors.grey,
size: 20,
),
onPressed: ()
setState(()
keyword = "";
);
,
),
fillColor: Colors.white,
filled: true,
),
inputFormatters: [],
//内容改变的回调
onChanged: (text)
print('change $text');
keyword = text;
// searchAroundAddress(text.toString());
,
//内容提交(按回车)的回调
onSubmitted: (text)
print('submit $text');
// 触发关闭弹出来的键盘。
keyword = text;
setState(()
isloading = true;
FocusScope.of(context).requestFocus(FocusNode());
);
searchAroundAddress(text.toString());
,
//按回车时调用
onEditingComplete: ()
print('onEditingComplete');
,
),flex: 8,),
SizedBox(width: 6,),
Expanded(child: InkWell(child: Text("搜索",style: TextStyle(color: Colors.white),),onTap: ()
print("搜索地点");
print(keyword);
searchAroundAddress(keyword.toString());
setState(()
);
,),flex: 1,)
],)
),
),
),
Container(
height: showType==0?300:1,
child: Stack(
children: <Widget>[
AmapView(
showZoomControl: false,
centerCoordinate: LatLng(30.250397, 120.213982),
maskDelay: Duration(milliseconds: 1000),
mask:Center(child: CircularProgressIndicator(),),
zoomLevel: 16,
autoRelease: true,
onMapCreated: (controller) async
_controller = controller;
// bool isPermission =await requestPermission();
// print(isPermission);
// if (await requestPermission())
final location = await AmapLocation.fetchLocation();
// setState(()
// _location=location;
// );
// print(location.province + location.city);
print('map show');
if (Platform.isAndroid)
await controller.showMyLocation(MyLocationOption(show: true));
await controller?.showLocateControl(true);
// final latLng = await _controller?.getLocation(interval: Duration(milliseconds: 60));
// await enableFluttifyLog(false); // 关闭log
_loadData(location.latLng);
// initData(location.address);
,
onMapMoveEnd: (MapMove move) async
_loadData(move.latLng);
,
),
Center(
child: Icon(
Icons.place,
size: 36.0,
color: Color(0xFFFF0000),
),
),
],
),
),
Expanded(
flex: 1,
child: Visibility(
visible: !isloading,
maintainSize: false,
maintainSemantics: false,
maintainInteractivity: false,
replacement: PageLoading(),
child: ListView.builder(
itemCount: list.length,
itemBuilder: (BuildContext context, int position)
// print("itemBuilder" + list.length.toString());
PoiModel item = list[position];
return InkWell(
child: Column(
children: <Widget>[
Container(
margin: EdgeInsets.only(top: 8, bottom: 5, left: 10),
child: Row(
children: <Widget>[
Icon(
Icons.place,
size: 20.0,
color: position == 0 ? Colors.green : Colors.grey,
),
Text(item.title,
style: TextStyle(
fontSize: 16, color: position == 0 ? Colors.green : Color(0xFF787878)))
],
),
),
Container(
margin: EdgeInsets.only(top: 5, bottom: 5, left: 18),
alignment: Alignment.centerLeft,
child: Text(
item.address,
style: TextStyle(
fontSize: 14,
color: Color(0xFF646464),
),
),
),
Divider(
height: 1,
)
],
),
onTap: () async
await _controller.setCenterCoordinate(item.latLng, zoomLevel: 16);
// print(item.provinceName+' '+item.cityName+' '+item.district);
// print(item);
// print(_location);
// if(position==0)
// Navigator.pop(
// context,
//
// 'title': _location.poiName,
// 'address': _location.address,
// 'provinceName': _location.province,
// 'cityName': _location.city,
// 'district': _location.district,
// 'latitude': item.latitude,
// 'longitude': item.longitude
// ,
// );
//
// else
Navigator.pop(
context,
'title': item.title=='当前位置'||item.title==''?item.address:item.title,
'address': item.address,
'provinceName': item.provinceName,
'cityName': item.cityName,
'district': item.district,
'latitude': item.latitude,
'longitude': item.longitude
,
);
,
);
),
),
),
],
),
);
void initData(String address) async
print("load map ini");
if(address==null)
Fluttertoast.showToast(
msg: "未知错误",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER,
timeInSecForIosWeb: 1,
backgroundColor: Colors.black38,
textColor: Colors.white,
fontSize: 16.0);
return;
setState(()
isloading = true;
);
// Unhandled Exception: type 'ReGeocode' is not a subtype of type 'List<Poi>' of 'value'
// Unhandled Exception: Exception: Error Domain=AMapSearchErrorDomain Code=1201 "MISSING_REQUIRED_PARAMS" UserInfo=NSLocalizedDescription=MISSING_REQUIRED_PARAMS
print(address);
final poiList = await AmapSearch.searchKeyword(
address??"钱江新城",
city: "杭州",
);
// poiModel = new PoiModel("当前位置", address, latLng, nowProvinceName, nowCityName, nowDistrictName, '', '');
list.clear();
list.add(poiModel);
// print(latLng.latitude);
// print(latLng.longitude);
for (var poi in poiList)
String title = poi.title;
String cityName = poi.cityName;
String provinceName = poi.provinceName;
// String address = poi.address;
String district = poi.adName;
LatLng latLng = poi.latLng;
// print(cityName+provinceName);
list.add(new PoiModel(title.toString(), provinceName.toString() + cityName.toString() + district, latLng,
provinceName, cityName, district, latLng.latitude.toStringAsFixed(7), latLng.longitude.toStringAsFixed(7)));
if(mounted)
setState(()
isloading = false;
);
void _loadData官方回复在安卓iOS中从app内通过androidamap://等协议唤醒手机上的高德地图app进行导航,高德平台会收费吗?高德地图URI API调用(安卓ios端)是否免费 - 商用5W一年
官方回复在安卓iOS中从app内通过androidamap://等协议唤醒手机上的高德地图app进行导航,高德平台会收费吗?高德地图URI API调用(安卓ios端)是否免费 - 商用5W一年