Flutter地址选择插件map_address_picker的使用
Flutter地址选择插件map_address_picker的使用
说明
map_address_picker
是一个简单的 Flutter 插件,用于通过 Google 地图选择用户的位置。
使用
要使用此插件,需要在 pubspec.yaml
文件中添加 map_address_picker
作为依赖项。
dependencies:
map_address_picker: ^x.x.x
开始使用
获取 API 密钥
- 访问 Google Cloud Platform 获取 API 密钥。
- 启用 Google Map SDK 以在每个平台上使用。
Android
- 在
android/app/build.gradle
中设置minSdkVersion
:
android {
defaultConfig {
minSdkVersion 20
}
}
- 在
android/app/src/main/AndroidManifest.xml
中指定 API 密钥:
<manifest ...>
<application ...
<meta-data android:name="com.google.android.geo.API_KEY"
android:value="YOUR KEY HERE"/>
</application>
</manifest>
iOS
- 在
ios/Runner/AppDelegate.m
中指定 API 密钥:
#import "AppDelegate.h"
#import "GeneratedPluginRegistrant.h"
#import "GoogleMaps/GoogleMaps.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[GMSServices provideAPIKey:@"YOUR KEY HERE"];
[GeneratedPluginRegistrant registerWithRegistry:self];
return [super application:application didFinishLaunchingWithOptions:launchOptions];
}
@end
或者在 Swift 代码中,在 ios/Runner/AppDelegate.swift
中指定 API 密钥:
import UIKit
import Flutter
import GoogleMaps
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GMSServices.provideAPIKey("YOUR KEY HERE")
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
示例使用
以下是一个简单的示例,展示如何使用 map_address_picker
插件来选择位置。
import 'package:flutter/material.dart';
import 'package:map_address_picker/map_address_picker.dart';
import 'package:map_address_picker/models/location_result.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Map Address Picker Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
darkTheme: ThemeData.dark(),
home: MyHomePage(title: 'Map Address Picker Example'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
[@override](/user/override)
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
LocationResult? locationResult;
_openLocationPicker() async {
var _result = await showLocationPicker(
context,
mapType: MapType.terrain,
requiredGPS: true,
automaticallyAnimateToCurrentLocation: true,
);
if (mounted) setState(() => locationResult = _result);
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
onPressed: _openLocationPicker,
child: Text("Pick Location"),
),
Text(
locationResult == null
? "null"
: "${locationResult!.latLng!.latitude}\n${locationResult!.latLng!.longitude}",
style: Theme.of(context).textTheme.headline4,
),
],
),
),
);
}
}
更多关于Flutter地址选择插件map_address_picker的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter地址选择插件map_address_picker的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
map_address_picker
是一个用于 Flutter 的地址选择插件,可以帮助用户在应用中通过地图选择地址。以下是使用 map_address_picker
的基本步骤:
1. 添加依赖
首先,在 pubspec.yaml
文件中添加 map_address_picker
依赖:
dependencies:
flutter:
sdk: flutter
map_address_picker: ^1.0.0 # 请根据最新版本号进行替换
然后运行 flutter pub get
来安装依赖。
2. 导入插件
在你的 Dart 文件中导入 map_address_picker
:
import 'package:map_address_picker/map_address_picker.dart';
3. 使用插件
在需要的地方调用 MapAddressPicker
来选择地址。以下是一个简单的示例:
import 'package:flutter/material.dart';
import 'package:map_address_picker/map_address_picker.dart';
class AddressPickerExample extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('地址选择示例'),
),
body: Center(
child: ElevatedButton(
onPressed: () async {
// 打开地址选择器
AddressPickerResult result = await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => MapAddressPicker(
apiKey: 'YOUR_GOOGLE_MAPS_API_KEY', // 替换为你的 Google Maps API 密钥
),
),
);
// 处理返回的地址信息
if (result != null) {
print('选择的地址: ${result.formattedAddress}');
print('纬度: ${result.latitude}');
print('经度: ${result.longitude}');
} else {
print('用户取消了选择');
}
},
child: Text('选择地址'),
),
),
);
}
}
4. 获取 Google Maps API 密钥
map_address_picker
需要使用 Google Maps API,因此你需要获取一个 Google Maps API 密钥。你可以通过以下步骤获取:
- 访问 Google Cloud Console.
- 创建一个新项目或选择现有项目。
- 启用
Maps SDK for Android
和Maps SDK for iOS
。 - 创建一个 API 密钥。
- 将生成的 API 密钥替换到代码中的
YOUR_GOOGLE_MAPS_API_KEY
。
5. 配置 Android 和 iOS 项目
确保在 AndroidManifest.xml
和 Info.plist
中正确配置了 Google Maps API 密钥。
Android (android/app/src/main/AndroidManifest.xml
):
<manifest ...>
<application ...>
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="YOUR_GOOGLE_MAPS_API_KEY"/>
</application>
</manifest>
iOS (ios/Runner/Info.plist
):
<key>io.flutter.embedded_views_preview</key>
<true/>
<key>NSLocationWhenInUseUsageDescription</key>
<string>我们需要您的位置信息来提供更好的服务。</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>我们需要您的位置信息来提供更好的服务。</string>
<key>GMSApiKey</key>
<string>YOUR_GOOGLE_MAPS_API_KEY</string>