Flutter 谷歌地图示例
一个演示 Google Maps 的 Flutter 应用程序。
插件
来自 Flutter 团队的 google_maps_flutter。
用法
要使用此插件,请将
google_maps_flutter:
git:
url: git://github.com/flutter/plugins
path: packages/google_maps_flutter
添加到您的 pubspec.yaml 文件中作为 依赖项。
入门
在 https://cloud.google.com/maps-platform/ 获取 API 密钥。
Android
在应用程序清单 android/app/src/main/AndroidManifest.xml 中指定您的 API 密钥
<manifest ...
<application ...
<meta-data android:name="com.google.android.geo.API_KEY"
android:value="YOUR KEY HERE"/>
iOS
在应用程序代理 ios/Runner/AppDelegate.m 中指定您的 API 密钥
Objective-C
#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
import UIKit
import Flutter
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
GMSServices.provideAPIKey("YOUR_API_KEY")
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
通过向应用的 Info.plist 文件添加一个布尔属性来选择加入嵌入式视图预览
键为 io.flutter.embedded_views_preview,值为 YES。
两者
您现在可以在小部件树中添加 GoogleMap 小部件。
地图视图可以通过传递给 GoogleMap 的 onMapCreated 回调的 GoogleMapController 来控制。GoogleMap 的 onMapCreated 回调。