Flutter 地点选择器
Flutter的位置选择器。
用法
要使用此插件,请将 place_picker 添加为 pubspec.yaml 文件中的依赖项。
入门
此软件包依赖 google_maps_flutter 来显示地图。请遵循以下指南为 Android 和 iOS 程序包添加 API 密钥。
如果您还没有 API 密钥,请在 https://cloud.google.com/maps-platform/ 获取。
Android
在应用程序清单 android/app/src/main/AndroidManifest.xml 中指定您的 API 密钥,并添加 ACCESS_FINE_LOCATION 权限
<manifest ...
<!-- Add this permission -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application ...
<!-- Add your api key here -->
<meta-data android:name="com.google.android.geo.API_KEY"
android:value="YOUR KEY HERE"/>
<activity ..../>
</application>
</manifest>
在您的 gradle.properties 文件中更新此项
android.enableJetifier=true
android.useAndroidX=true
org.gradle.jvmargs=-Xmx1536M
请同时确保您的 build.gradle 中有这些依赖项
// parent level build.gradle (android/build.gradle)
dependencies {
classpath 'com.android.tools.build:gradle:3.3.0'
classpath 'com.google.gms:google-services:4.2.0'
}
...
// app level build.gradle (android/app/build.gradle)
compileSdkVersion 28
iOS
在应用程序代理 ios/Runner/AppDelegate.m 中指定您的 API 密钥
#include "AppDelegate.h"
#include "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: [UIApplicationLaunchOptionsKey: Any]?
) -> Bool {
GMSServices.provideAPIKey("YOUR KEY HERE")
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
通过向应用的 Info.plist 文件添加一个布尔属性来选择加入嵌入式视图预览
键为 io.flutter.embedded_views_preview,值为 YES。

还要将这些添加到 Info.plist 中的 dict 值,以便位置请求在 iOS 上正常工作

示例用法
将包导入到您的代码中
import 'package:place_picker/place_picker.dart';
创建一个类似下面的方法,并在按钮或 InkWell 的 onTap 中调用它。将返回一个 LocationResult
包含所选地点的名称和纬度/经度。然后您可以以任何您想要的方式处理结果。
void showPlacePicker() async {
LocationResult result = await Navigator.of(context).push(MaterialPageRoute(
builder: (context) =>
PlacePicker("YOUR API KEY")));
// Handle the result in your way
print(result);
}