Flutter地图搜索插件gxcm_amap_search的使用
Flutter地图搜索插件gxcm_amap_search的使用
gxcm_amap_search
这是一个用于在Flutter应用中实现地图搜索功能的插件。它支持通过关键词搜索和周边搜索来获取地理位置信息。
使用步骤
1. 初始化插件
在使用插件之前,需要设置高德地图的API Key并同意隐私政策。
TextButton.icon(
onPressed: () {
// 设置高德地图API Key
GxcmAmapSearch.setApiKey('f53141bcada3bd03fd79c37f652cd45f', 'df6898859be82405b9b41d8d1f1e86d3');
// 同意隐私政策
GxcmAmapSearch.updatePrivacyAgree(true);
GxcmAmapSearch.updatePrivacyShow(true, true);
},
icon: const Icon(Icons.login),
label: const Text('注册id'),
)
2. 关键词搜索
通过输入关键词(如“餐厅”、“学校”等)和城市名称,可以搜索相关的地理位置信息。
Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
children: [
Expanded(
child: TextField(
controller: textEditingController,
),
),
TextButton.icon(
onPressed: () async {
// 执行关键词搜索
dataList = await GxcmAmapSearch.searchKeyword(
keyword: textEditingController.text,
city: "深圳市",
types: "餐饮服务|商务住宅|生活服务|风景名胜|购物服务|科教文化服务|公司企业|政府机构及社会团体|道路附属设施|地名地址信息|公共设施",
);
setState(() {}); // 更新UI
},
icon: const Icon(Icons.search),
label: const Text('关键词搜索'),
),
],
),
)
3. 周边搜索
通过指定经纬度、搜索类型和关键词,可以获取附近的地理位置信息。
TextButton.icon(
onPressed: () async {
// 执行周边搜索
dataList = await GxcmAmapSearch.searchAround(
latitude: 22.600323788793503, // 纬度
longitude: 114.10581646859714, // 经度
types: "餐饮服务|商务住宅|生活服务|风景名胜|购物服务|科教文化服务|公司企业|政府机构及社会团体|道路附属设施|地名地址信息|公共设施",
keyword: textEditingController.text,
);
setState(() {}); // 更新UI
},
icon: const Icon(Icons.search),
label: const Text('周边搜索'),
)
4. 显示搜索结果
将搜索到的数据列表展示在一个可滚动的列表视图中。
Expanded(
child: ListView.builder(
itemBuilder: (context, index) {
return ListTile(
title: Text(dataList[index].title), // 地理位置名称
subtitle: Text(dataList[index].snippet), // 描述信息
trailing: Text(dataList[index].cityName), // 城市名称
);
},
itemCount: dataList.length, // 数据长度
),
)
完整示例代码
以下是一个完整的示例代码,展示了如何集成和使用gxcm_amap_search插件。
import 'package:flutter/material.dart';
import 'package:gxcm_amap_search/SearchResultItem.dart';
import 'package:gxcm_amap_search/gxcm_amap_search.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
[@override](/user/override)
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final textEditingController = TextEditingController();
List<SearchResultItem> dataList = [];
[@override](/user/override)
void initState() {
super.initState();
}
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('地图搜索插件示例'),
),
body: Column(
children: [
// 注册API Key
TextButton.icon(
onPressed: () {
GxcmAmapSearch.setApiKey('f53141bcada3bd03fd79c37f652cd45f', 'df6898859be82405b9b41d8d1f1e86d3');
GxcmAmapSearch.updatePrivacyAgree(true);
GxcmAmapSearch.updatePrivacyShow(true, true);
},
icon: const Icon(Icons.login),
label: const Text('注册id'),
),
// 输入框和搜索按钮
Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
children: [
Expanded(
child: TextField(
controller: textEditingController,
),
),
TextButton.icon(
onPressed: () async {
dataList = await GxcmAmapSearch.searchKeyword(
keyword: textEditingController.text,
city: "深圳市",
types: "餐饮服务|商务住宅|生活服务|风景名胜|购物服务|科教文化服务|公司企业|政府机构及社会团体|道路附属设施|地名地址信息|公共设施",
);
setState(() {}); // 更新UI
},
icon: const Icon(Icons.search),
label: const Text('关键词搜索'),
),
],
),
),
// 周边搜索按钮
TextButton.icon(
onPressed: () async {
dataList = await GxcmAmapSearch.searchAround(
latitude: 22.600323788793503,
longitude: 114.10581646859714,
types: "餐饮服务|商务住宅|生活服务|风景名胜|购物服务|科教文化服务|公司企业|政府机构及社会团体|道路附属设施|地名地址信息|公共设施",
keyword: textEditingController.text,
);
setState(() {}); // 更新UI
},
icon: const Icon(Icons.search),
label: const Text('周边搜索'),
),
// 搜索结果列表
Expanded(
child: ListView.builder(
itemBuilder: (context, index) {
return ListTile(
title: Text(dataList[index].title), // 地理位置名称
subtitle: Text(dataList[index].snippet), // 描述信息
trailing: Text(dataList[index].cityName), // 城市名称
);
},
itemCount: dataList.length, // 数据长度
),
),
],
),
),
);
}
}
更多关于Flutter地图搜索插件gxcm_amap_search的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter地图搜索插件gxcm_amap_search的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
gxcm_amap_search 是一个 Flutter 插件,用于在 Flutter 应用中集成高德地图的搜索功能。它提供了多种搜索功能,如关键字搜索、周边搜索、地理编码、逆地理编码等。以下是如何在 Flutter 项目中使用 gxcm_amap_search 插件的基本步骤。
1. 添加依赖
首先,你需要在 pubspec.yaml 文件中添加 gxcm_amap_search 插件的依赖。
dependencies:
flutter:
sdk: flutter
gxcm_amap_search: ^版本号 # 替换为最新版本号
然后运行 flutter pub get 来安装依赖。
2. 配置高德地图 SDK
在使用 gxcm_amap_search 之前,你需要在 高德开放平台 注册一个开发者账号,并创建一个应用以获取 API Key。
Android 配置
在 android/app/src/main/AndroidManifest.xml 文件中添加以下配置:
<meta-data
android:name="com.amap.api.v2.apikey"
android:value="你的高德地图API Key" />
iOS 配置
在 ios/Runner/Info.plist 文件中添加以下配置:
<key>AMapServices</key>
<dict>
<key>apiKey</key>
<string>你的高德地图API Key</string>
</dict>
3. 初始化插件
在使用插件之前,你需要在 main.dart 或其他适当的地方初始化插件。
import 'package:gxcm_amap_search/gxcm_amap_search.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await AMapSearch.instance.init('你的高德地图API Key');
runApp(MyApp());
}
4. 使用搜索功能
gxcm_amap_search 提供了多种搜索功能,以下是一些常见的使用示例。
关键字搜索
import 'package:gxcm_amap_search/gxcm_amap_search.dart';
void searchByKeyword() async {
AMapSearchRequest request = AMapSearchRequest(
keywords: '餐厅',
city: '北京',
);
AMapSearchResponse response = await AMapSearch.instance.search(request);
if (response.isSuccess) {
// 处理搜索结果
for (var poi in response.pois) {
print('Name: ${poi.name}, Address: ${poi.address}');
}
} else {
print('搜索失败: ${response.message}');
}
}
周边搜索
void searchAround() async {
AMapSearchRequest request = AMapSearchRequest(
location: AMapLocation(latitude: 39.90469, longitude: 116.40717),
keywords: '餐厅',
radius: 1000,
);
AMapSearchResponse response = await AMapSearch.instance.search(request);
if (response.isSuccess) {
// 处理搜索结果
for (var poi in response.pois) {
print('Name: ${poi.name}, Address: ${poi.address}');
}
} else {
print('搜索失败: ${response.message}');
}
}
地理编码(地址转坐标)
void geocode() async {
AMapGeocodeRequest request = AMapGeocodeRequest(
address: '北京市朝阳区望京SOHO',
city: '北京',
);
AMapGeocodeResponse response = await AMapSearch.instance.geocode(request);
if (response.isSuccess) {
// 处理地理编码结果
for (var geocode in response.geocodes) {
print('Latitude: ${geocode.location.latitude}, Longitude: ${geocode.location.longitude}');
}
} else {
print('地理编码失败: ${response.message}');
}
}
逆地理编码(坐标转地址)
void reverseGeocode() async {
AMapReverseGeocodeRequest request = AMapReverseGeocodeRequest(
location: AMapLocation(latitude: 39.90469, longitude: 116.40717),
);
AMapReverseGeocodeResponse response = await AMapSearch.instance.reverseGeocode(request);
if (response.isSuccess) {
// 处理逆地理编码结果
print('Address: ${response.address}');
} else {
print('逆地理编码失败: ${response.message}');
}
}
5. 处理权限
在使用地图和位置相关的功能时,确保你已经在 Android 和 iOS 上正确配置了权限。
Android
在 android/app/src/main/AndroidManifest.xml 中添加以下权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
iOS
在 ios/Runner/Info.plist 中添加以下权限:
<key>NSLocationWhenInUseUsageDescription</key>
<string>需要获取您的位置信息以提供更好的服务</string>

