Flutter地点搜索插件google_search_place的使用
Flutter地点搜索插件google_search_place的使用
插件介绍
google_search_place
是一个用于在所有平台上实现自定义Google地点自动完成的小部件。
开始使用
添加依赖
你可以通过以下命令添加 google_search_place
作为依赖项,并使用最新稳定版本:
$ dart pub add google_search_place
或者你可以在 pubspec.yaml
文件中的 dependencies
部分手动添加 google_search_place
:
dependencies:
google_search_place: ^replace-with-latest-version
最新版本是:
包括预发布版本的最新版本是:
在升级之前请注意:
- 主版本和次版本可能会发生破坏性更改。
- 查看[迁移指南][迁移指南]以获取完整的破坏性更改列表。
超简单的使用方法
import 'package:google_search_place/google_search_place.dart';
final TextEditingController _searchPlaceController = TextEditingController();
SearchPlaceAutoCompletedTextField(
googleAPIKey: "", // 替换为你的 Google API 密钥
controller: _searchPlaceController,
itmOnTap: (Prediction prediction) {
_searchPlaceController.text = prediction.description ?? ""; // 设置搜索结果的描述
_searchPlaceController.selection = TextSelection.fromPosition(TextPosition(offset: prediction.description?.length ?? 0)); // 设置光标位置
},
getPlaceDetailWithLatLng: (Prediction prediction) {
_searchPlaceController.text = prediction.description ?? ""; // 设置搜索结果的描述
_searchPlaceController.selection = TextSelection.fromPosition(TextPosition(offset: prediction.description?.length ?? 0)); // 设置光标位置
debugPrint("${prediction.lat} ${prediction.lng}"); // 打印地点的经纬度
}
)
自定义选项
你可以自定义文本字段输入装饰和防抖时间。
完整示例代码
以下是完整的示例代码,展示了如何使用 google_search_place
插件:
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:google_search_place/google_search_place.dart';
import 'package:google_search_place/model/prediction.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: '搜索Google地点',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: '搜索Google地点'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
[@override](/user/override)
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final TextEditingController _searchPlaceController = TextEditingController();
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: SearchPlaceAutoCompletedTextField(
googleAPIKey: "GOOGLE_API_KEY", // 替换为你的 Google API 密钥
controller: _searchPlaceController,
itmOnTap: (Prediction prediction) {
_searchPlaceController.text = prediction.description ?? ""; // 设置搜索结果的描述
_searchPlaceController.selection = TextSelection.fromPosition(
TextPosition(
offset: prediction.description?.length ?? 0)); // 设置光标位置
},
getPlaceDetailWithLatLng: (Prediction prediction) {
_searchPlaceController.text = prediction.description ?? ""; // 设置搜索结果的描述
_searchPlaceController.selection = TextSelection.fromPosition(
TextPosition(
offset: prediction.description?.length ?? 0)); // 设置光标位置
// 获取搜索地点的经纬度
debugPrint("${prediction.lat} ${prediction.lng}");
// 获取地点详情
debugPrint("地点详情 : ${prediction.placeDetails}");
},
),
),
),
);
}
}
更多关于Flutter地点搜索插件google_search_place的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter地点搜索插件google_search_place的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,关于Flutter中google_maps_place_picker
(注意:google_search_place
可能是一个不准确的名称,因为Flutter社区中更常用的是google_maps_place_picker
用于地点搜索和选择)的使用,以下是一个基本的代码示例,展示了如何在Flutter应用中集成并使用该插件来进行地点搜索。
首先,确保你已经在pubspec.yaml
文件中添加了依赖项:
dependencies:
flutter:
sdk: flutter
google_maps_flutter: ^2.1.1 # 用于显示地图
google_maps_place_picker: ^2.0.0-nullsafety.3 # 用于地点搜索和选择
然后,运行flutter pub get
来安装依赖项。
接下来,在你的Flutter应用中,你可以按照以下步骤实现地点搜索功能:
- 导入必要的包:
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:google_maps_place_picker/google_maps_place_picker.dart';
- 定义必要的变量:
class _MyHomePageState extends State<MyHomePage> {
late GoogleMapsPlacePickerController _controller;
Place? _selectedPlace;
@override
void initState() {
super.initState();
_controller = GoogleMapsPlacePickerController();
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
- 构建UI:
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('地点搜索示例'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
_selectedPlace == null
? Text('选择一个地点')
: Text('选择的地点: ${_selectedPlace!.name}'),
SizedBox(height: 20),
ElevatedButton(
onPressed: () async {
final Result? result = await _controller.showPicker(
context,
apiKey: 'YOUR_GOOGLE_MAPS_API_KEY', // 请替换为你的Google Maps API密钥
initialPosition: LatLng(37.7749, -122.4194), // 初始位置,例如旧金山
useCurrentLocation: true,
selectionMode: SelectionMode.single,
);
if (result != null) {
if (result.runtimeType == PlaceResult) {
setState(() {
_selectedPlace = result.place;
});
}
}
},
child: Text('搜索地点'),
),
],
),
),
);
}
}
- 完整的主函数:
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
在上面的代码中,我们创建了一个简单的Flutter应用,其中包含一个按钮,用于打开地点选择器。用户可以选择一个地点,该地点将被显示在按钮下方的文本中。
请注意,你需要将'YOUR_GOOGLE_MAPS_API_KEY'
替换为你自己的Google Maps API密钥。确保你已经在Google Cloud Platform上启用了Places API,并获得了相应的API密钥。
这个示例展示了如何使用google_maps_place_picker
插件进行基本的地点搜索和选择。根据你的需求,你可以进一步自定义和扩展这个示例。