请按照本指南中的步骤下载 Places SDK for iOS,将库及其依赖项添加到您的应用中,并获取免费 API 密钥。
每个版本都有相应的版本说明。
第 1 步:获取最新版本的 Xcode
如需使用 Places SDK for iOS 构建项目,您需要:
第 2 步:安装 SDK
要在新项目中安装该 API,请按以下步骤操作:使用 Cocoapods
Places SDK for iOS 可用作 CocoaPod Pod GooglePlaces,其中包含所有地点功能。
CocoaPods 是一种用于 Swift 和 Objective-C Cocoa 项目的开源依赖项管理器。如果您尚未安装 CocoaPods 工具,请在 macOS 上从终端运行以下命令进行安装。如需了解详情,请参阅 CocoaPods 入门指南。
sudo gem install cocoapods
为 Places SDK for iOS 创建一个 Podfile
,并使用它来安装 SDK 及其依赖项:
- 如果您尚未创建 Xcode 项目,请立即创建一个并将其保存到您的本地计算机上。如果您刚开始接触 iOS 开发,请创建一个新项目,然后选择 iOS 应用模板。
- 在您的项目目录中创建一个名为
Podfile
的文件。此文件可定义您项目的依赖项。 - 修改
Podfile
并添加依赖项及其版本。以下示例会指定您的应用目标名称和GooglePlaces
pod 的名称:source 'https://github.com/CocoaPods/Specs.git' platform :ios, '13.0' target 'YOUR_APPLICATION_TARGET_NAME_HERE' do pod 'GooglePlaces', '7.4.0' end
请务必定期运行pod outdated
,以检查是否有更新的版本,从而确保您始终使用的是最新版本。 - 保存
Podfile
。 打开一个终端,然后转到包含
Podfile
的目录:cd <path-to-project>
运行
pod install
命令。此操作会安装Podfile
中指定的 API 及其可能具有的任何依赖项。pod install
关闭 Xcode,然后打开(双击)您项目的
.xcworkspace
文件以启动 Xcode。从此刻开始,您必须使用.xcworkspace
文件打开项目。
如需更新现有项目的 API,请按以下步骤操作:
- 打开终端,然后转到包含
Podfile
的项目目录。 - 运行
pod update
命令。此操作会将Podfile
中指定的所有 API 更新为最新版本。
使用 Carthage
Places SDK for iOS 可与 Carthage 搭配使用,Carthage 是一个简单的分散式依赖项管理器,适用于 Swift 和 Objective-C Cocoa 项目。
- 安装 Carthage。有多种方法,请参阅 Carthage README 文件,了解具体步骤。
- 如果您尚未创建 Xcode 项目,请立即创建一个并将其保存到您的本地计算机上。如果您刚开始接触 iOS 开发,请创建一个新项目,然后选择 iOS 应用模板。
- 在您的项目目录中创建一个名为
Cartfile
的文件。此文件定义项目的依赖项。 - 修改
Cartfile
并添加依赖项及其版本:
binary "https://dl.google.com/geosdk/GooglePlaces.json" == 6.2.1
请务必定期运行carthage outdated
以检测何时有新版本,从而确保您始终使用的是最新版本。 - 保存
Cartfile
。 - 在终端窗口中,转到包含
Cartfile
的目录:
cd <path-to-project>
- 运行
carthage update
命令。此操作会安装Cartfile
中指定的 API 及其可能具有的任何依赖项。 - 在 Finder 的项目目录中,转到
Carthage/Build/iOS
下的已下载框架文件。 - 将以下捆绑包拖动到您的项目中(出现提示时,选择 Copy items if needed):
GooglePlaces-x.x.x/Frameworks/GooglePlaces.xcframework
- 右键点击项目中的
GooglePlaces.xcframework
,然后选择 Show In Finder(在查找器中显示)。 - 将
GooglePlaces.bundle
从Resources
文件夹拖动到项目中。系统显示提示时,确保没有选中“Copy items into destination group's folder”(将项目复制到目标组的文件夹)。 - 从项目导航器中选择项目,然后选择应用的目标。
- 打开 Build Phases 标签,然后在 Link Binary with Libraries 中,添加以下框架和库:
CoreGraphics.framework
CoreLocation.framework
libc++.tbd
libz.tbd
QuartzCore.framework
UIKit.framework
选择您的项目(而不是特定目标),然后打开 Build Settings 标签页。
- 在 Other Linker Flags(其他链接器标记)部分中,添加
-ObjC
。如果看不到这些设置,请将构建设置栏中的过滤条件从基本更改为全部。
如需更新现有项目的 API,请按以下步骤操作:
- 打开终端,然后转到包含
Cartfile
的项目目录。 - 运行
carthage update
命令。此操作会将Cartfile
中指定的所有 API 更新为最新版本。
手动安装
本指南介绍了如何在您的项目中手动添加 GooglePlaces 框架,以及如何在 Xcode 中配置您的 build 设置。
- 下载 SDK 源文件:GooglePlaces-7.4.0
- 解压缩源文件。
- 启动 Xcode,并打开现有的项目或创建新项目。如果您刚开始接触 iOS 开发,请创建一个新项目,然后选择 iOS 应用模板。
- 从您的项目中移除之前版本中的所有地图包。
- 将以下捆绑包拖动到您的项目中(出现提示时,选择 Copy items if needed):
GooglePlaces-x.x.x/Frameworks/GooglePlaces.xcframework
- 右键点击项目中的
GooglePlaces.xcframework
,然后选择 Show In Finder(在查找器中显示)。 - 将
GooglePlaces.bundle
从Resources
文件夹拖动到项目中。系统显示提示时,确保没有选中“Copy items into destination group's folder”(将项目复制到目标组的文件夹)。 - 从项目导航器中选择项目,然后选择应用的目标。
- 打开 Build Phases 标签,然后在 Link Binary with Libraries 中,添加以下框架和库:
CoreGraphics.framework
CoreLocation.framework
libc++.tbd
libz.tbd
QuartzCore.framework
UIKit.framework
选择您的项目(而不是特定目标),然后打开 Build Settings 标签页。
- 在 Other Linker Flags(其他链接器标记)部分中,添加
-ObjC
。如果看不到这些设置,请将构建设置栏中的过滤条件从基本更改为全部。
安装 XCFramework
XCFramework 是一个二进制软件包,可用于多个平台(包括使用 M1 芯片组的机器),以安装 Places SDK for iOS。本指南介绍了如何将包含 Places SDK for iOS 的 XCFramework 添加到您的项目中,以及如何在 Xcode 中配置您的 build 设置。
带 Carthage
Places SDK for iOS 可与 Carthage 搭配使用,Carthage 是一个简单的分散式依赖项管理器,适用于 Swift 和 Objective-C Cocoa 项目。
- 安装 Carthage。有多种方法,请参阅 Carthage README 文件,了解具体步骤。
- 如果您还没有 Xcode 项目,请立即创建一个并将其保存到您的本地机器。如果您刚开始接触 iOS 开发,请创建一个新项目,然后选择 iOS 应用模板。
- 在您的项目目录中创建一个名为
Cartfile
的文件。此文件定义项目的依赖项。 修改
Cartfile
并添加依赖项及其版本:binary "https://dl.google.com/geosdk/GooglePlaces.json" == 6.2.1-beta
- 保存
Cartfile
。 在终端窗口中,转到包含
Cartfile
的目录:cd <path-to-project>
- 运行
carthage update
命令。此操作会安装Cartfile
中指定的 API 及其可能具有的任何依赖项。 - 在 Finder 的项目目录中,转到
Carthage/Build
下的已下载框架文件。 - 将以下 XCFramework 拖到框架、库和嵌入内容下的项目中。请务必选择不嵌入:
GooglePlaces-x.x.x/GooglePlaces.xcframework
- 右键点击项目中的
GooglePlaces.xcframework
,然后选择 Show In Finder。 - 将
GooglePlaces.bundle
从ios-arm64/GooglePlaces.framework/Resources
文件夹拖动到 Xcode 项目的顶层目录中。出现提示时,确保未选择将项目复制到目标组的文件夹中。 - 从 Project Navigator 中选择您的项目,然后选择应用的目标。
- 打开 Build Phases(构建阶段)标签页,然后在 Link Binary with Libraries(将二进制文件链接到库)中添加以下框架和库:
CoreGraphics.framework
CoreLocation.framework
libc++.tbd
libz.tbd
QuartzCore.framework
UIKit.framework
选择您的项目(而不是特定目标),然后打开 Build Settings 标签页。
- 在 Other Linker Flags(其他链接器标记)部分中,添加
-ObjC
。如果看不到这些设置,请将构建设置栏中的过滤条件从基本更改为全部。
手动
本指南介绍了如何将 Maps SDK for iOS 手动添加到您的项目中,并配置 Xcode 中的构建设置。
- 下载 SDK 源文件:GooglePlaces-7.4.0。
- 解压缩源文件。转到 Frameworks 文件夹以访问 XCFramework。
- 启动 Xcode 并打开现有项目,或创建新项目。如果您是 iOS 新手,请创建一个新项目并选择 iOS 应用模板。
- 将以下 XCFramework 拖到框架、库和嵌入内容下的项目中。请务必选择不嵌入:
GooglePlaces-x.x.x/GooglePlaces.xcframework
- 右键点击项目中的
GooglePlaces.xcframework
,然后选择 Show In Finder。 - 将
GooglePlaces.bundle
从ios-arm64/GooglePlaces.framework/Resources
文件夹拖动到 Xcode 项目的顶层目录中。出现提示时,确保未选择将项目复制到目标组的文件夹中。 - 从 Project Navigator 中选择您的项目,然后选择应用的目标。
- 打开 Build Phases(构建阶段)标签页,然后在 Link Binary with Libraries(将二进制文件链接到库)中添加以下框架和库:
CoreGraphics.framework
CoreLocation.framework
libc++.tbd
libz.tbd
QuartzCore.framework
UIKit.framework
选择您的项目(而不是特定目标),然后打开 Build Settings 标签页。
- 在 Other Linker Flags(其他链接器标记)部分中,添加
-ObjC
。如果看不到这些设置,请将构建设置栏中的过滤条件从基本更改为全部。
第 3 步:将 API 密钥添加到您的应用
在以下示例中,将 YOUR_API_KEY
替换为您的 API 密钥。
Swift
按照以下方法向 AppDelegate.swift
添加 API 密钥:
- 添加以下 import 语句:
import GooglePlaces
- 将以下内容添加到您的
application(_:didFinishLaunchingWithOptions:)
方法中,注意将 YOUR_API_KEY 替换为您的 API 密钥:GMSPlacesClient.provideAPIKey("YOUR_API_KEY")
Objective-C
按照以下方法向 AppDelegate.m
添加 API 密钥:
- 添加以下 import 语句:
@import GooglePlaces;
- 将以下内容添加到您的
application:didFinishLaunchingWithOptions:
方法中,注意将 YOUR_API_KEY 替换为您的 API 密钥:[GMSPlacesClient provideAPIKey:@"YOUR_API_KEY"];
第 4 步:开始编写代码
以下代码示例演示了如何获取当前地点。
Swift
import GooglePlaces import UIKit class GetStartedViewController : UIViewController { // Add a pair of UILabels in Interface Builder, and connect the outlets to these variables. @IBOutlet private var nameLabel: UILabel! @IBOutlet private var addressLabel: UILabel! private var placesClient: GMSPlacesClient! override func viewDidLoad() { super.viewDidLoad() placesClient = GMSPlacesClient.shared() } // Add a UIButton in Interface Builder, and connect the action to this function. @IBAction func getCurrentPlace(_ sender: UIButton) { let placeFields: GMSPlaceField = [.name, .formattedAddress] placesClient.findPlaceLikelihoodsFromCurrentLocation(withPlaceFields: placeFields) { [weak self] (placeLikelihoods, error) in guard let strongSelf = self else { return } guard error == nil else { print("Current place error: \(error?.localizedDescription ?? "")") return } guard let place = placeLikelihoods?.first?.place else { strongSelf.nameLabel.text = "No current place" strongSelf.addressLabel.text = "" return } strongSelf.nameLabel.text = place.name strongSelf.addressLabel.text = place.formattedAddress } } }
Objective-C
#import "GetStartedViewController.h" @import GooglePlaces; @interface GetStartedViewController () // Add a pair of UILabels in Interface Builder and connect the outlets to these variables @property (weak, nonatomic) IBOutlet UILabel *nameLabel; @property (weak, nonatomic) IBOutlet UILabel *addressLabel; @end @implementation GetStartedViewController { GMSPlacesClient *_placesClient; } - (void)viewDidLoad { [super viewDidLoad]; _placesClient = [GMSPlacesClient sharedClient]; } // Add a pair of UILabels in Interface Builder and connect the outlets to these variables. - (IBAction)getCurrentPlace:(UIButton *)sender { GMSPlaceField placeFields = (GMSPlaceFieldName | GMSPlaceFieldFormattedAddress); __weak typeof(self) weakSelf = self; [_placesClient findPlaceLikelihoodsFromCurrentLocationWithPlaceFields:placeFields callback:^(NSArray<GMSPlaceLikelihood *> * _Nullable likelihoods, NSError * _Nullable error) { __typeof__(self) strongSelf = weakSelf; if (strongSelf == nil) { return; } if (error != nil) { NSLog(@"An error occurred %@", [error localizedDescription]); return; } GMSPlace *place = likelihoods.firstObject.place; if (place == nil) { strongSelf.nameLabel.text = @"No current place"; strongSelf.addressLabel.text = @""; return; } strongSelf.nameLabel.text = place.name; strongSelf.addressLabel.text = place.formattedAddress; }]; } @end
后续步骤
配置项目后,您可以浏览示例应用。 您需要安装 Cocoapods v1.6.1。