Flutter如何通过高德地图实现POI附近地址搜索功能
在Flutter中如何集成高德地图SDK并实现POI附近地址搜索功能?目前按照官方文档配置后,地图能正常显示,但调用搜索API时总是返回空结果。请问:1) 是否需要额外申请搜索权限?2) 正确的POI搜索参数应该如何配置(特别是关键词、范围和排序规则)?3) 有没有完整的Dart代码示例可以参考?
2 回复
使用高德地图SDK,通过AMapSearchKit实现POI搜索。步骤:
- 配置高德API Key。
- 创建
AMapSearchAPI实例。 - 构建
POIAroundSearchRequest,设置中心点坐标、关键字和搜索半径。 - 调用
aMapPOIAroundSearch发起请求。 - 在回调中处理搜索结果。
更多关于Flutter如何通过高德地图实现POI附近地址搜索功能的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
在Flutter中通过高德地图实现POI附近地址搜索功能,可以使用高德官方提供的amap_flutter_map和amap_flutter_location插件。以下是具体实现步骤:
1. 添加依赖
在pubspec.yaml中添加:
dependencies:
amap_flutter_map: ^3.0.0
amap_flutter_location: ^3.0.0
permission_handler: ^11.0.0 # 用于权限处理
2. 配置Android和iOS
Android:
- 在
AndroidManifest.xml中添加权限和API Key:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application>
<meta-data
android:name="com.amap.api.v2.apikey"
android:value="您的Android API Key"/>
</application>
iOS:
- 在
Info.plist中添加权限描述和API Key:
<key>NSLocationWhenInUseUsageDescription</key>
<string>需要定位权限以实现附近搜索</string>
<key>AMapApiKey</key>
<string>您的iOS API Key</string>
3. 实现POI搜索功能
使用AMapSearch类进行POI搜索:
import 'package:amap_flutter_location/amap_flutter_location.dart';
import 'package:amap_flutter_map/amap_flutter_map.dart';
import 'package:permission_handler/permission_handler.dart';
class POISearchPage extends StatefulWidget {
@override
_POISearchPageState createState() => _POISearchPageState();
}
class _POISearchPageState extends State<POISearchPage> {
final AMapController _mapController = AMapController();
final AMapLocation _currentLocation = AMapLocation();
List<PoiItem> _poiList = [];
@override
void initState() {
super.initState();
_requestPermission();
}
// 请求定位权限
void _requestPermission() async {
await Permission.locationWhenInUse.request();
_getCurrentLocation();
}
// 获取当前位置
void _getCurrentLocation() {
AMapFlutterLocation.getLocation((Location location) {
setState(() {
_currentLocation.latitude = location.lat;
_currentLocation.longitude = location.lng;
});
_searchNearbyPOI();
});
}
// 搜索附近POI
void _searchNearbyPOI() async {
final search = AMapSearch();
final result = await search.searchAround(
center: LatLng(
_currentLocation.latitude!,
_currentLocation.longitude!
),
keyWord: "餐饮", // 搜索关键词
radius: 1000, // 搜索半径(米)
);
setState(() {
_poiList = result.poiList ?? [];
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
Expanded(
child: AMapWidget(
onMapCreated: (controller) {
_mapController = controller;
},
),
),
// 显示搜索结果列表
Container(
height: 200,
child: ListView.builder(
itemCount: _poiList.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(_poiList[index].title ?? ''),
subtitle: Text(_poiList[index].address ?? ''),
);
},
),
),
],
),
floatingActionButton: FloatingActionButton(
onPressed: _searchNearbyPOI,
child: Icon(Icons.search),
),
);
}
}
4. 关键说明
- 权限处理:必须获取定位权限才能使用位置相关功能
- API Key:需要在高德开放平台申请并配置正确的Key
- 搜索参数:可调整搜索半径、关键词等参数
- 错误处理:建议添加try-catch处理网络请求异常
5. 扩展功能
- 添加搜索框实现关键词搜索
- 实现分页加载更多结果
- 添加POI详情展示页面
- 集成路线规划功能
这样就实现了基于当前位置的附近POI搜索功能。记得在实际使用中处理各种边界情况和错误状态。

