简介
TPLPlaces iOS SDK 允许您构建能够响应设备周围本地企业和其他地点的定位应用程序。TPLPlaces iOS SDK 的自动补全服务在用户输入时根据用户的搜索查询返回地点建议。
入门
在您可以使用 TPLMaps 平台 API 和 SDK 之前,您必须注册并创建一个 计费账户。
步骤 1:获取最新版本的 Xcode
要使用 TPLPlaces iOS SDK 构建项目,您需要 Xcode 的 11.0 或更高版本。
步骤 2:生成 tplservices 文件
要使用 TPLPlaces iOS SDK,您必须获取 tplservices.plist 文件,然后将其添加到您的 iOS 应用程序中。该 tplservices.plist 用于跟踪与您的项目相关的 API 请求以进行使用和计费。
获取最新的 tplservices 文件 在这里。
该文件包含您刚刚启用服务的配置详细信息,例如密钥和标识符。将 tplservices.plist 文件添加到从 TPLMaps 门户下载的 Xcode 项目中,以开始使用 TPLMaps 平台 API 和 SDK。
第3步:安装 SDK
TPLPlaces 的 iOS SDK 以 CocoaPods pod 的形式提供。CocoaPods 是一个用于 Swift 和 Objective-C Cocoa 项目的开源依赖关系管理器。
如果您还没有 CocoaPods 工具,请在 macOS 上通过终端运行以下命令进行安装。有关详细信息,请参阅 CocoaPods 入门指南。
sudo gem install cocoapods
为 iOS 的 TPLMaps SDK 创建一个 Podfile
,并使用它安装 API 及其依赖项。
-
如果您还没有 Xcode 项目,现在创建一个并保存到您的本地计算机。(如果您是 iOS 开发的新手,请创建一个单视图应用程序。)
-
在项目目录中创建一个名为
Podfile
的文件。该文件定义了项目的依赖项。 -
编辑
Podfile
并添加您的依赖项。以下是一个例子,其中包括您需要用于 iOS TPLPlaces SDK 的依赖项
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.3'
target 'YOUR_APPLICATION_TARGET_NAME_HERE' do
pod 'TPLPlaces'
end
-
保存
Podfile
-
打开终端,转到包含
Podfile
的目录。
cd <path-to-project>
-
运行
pod install
命令。这将安装Podfile
中指定的 API 以及可能具有的任何依赖项。 -
关闭 Xcode,然后打开(双击)项目
.xcworkspace
文件以启动 Xcode。从现在开始,您必须使用.xcworkspace
文件来打开项目。
现在您已经将 TPLPlaces SDK 集成到 iOS 项目中,让我们向默认视图控制器添加一个 自动完成 UI 控件。
添加全屏控件
当您想要一个模态上下文时,可以使用全屏控件,此时自动完成 UI 会暂时替换您的应用程序的 UI,直到用户做出选择。此功能由 TPLAutocompleteViewController 类提供。当用户选择一个地点时,您的应用程序将收到一个回调。
要向您的应用程序添加全屏控件
- 在主应用程序中创建一个 UI 元素来启动自动完成 UI 控件,例如在
UIButton
上的触摸处理程序。 - 在父视图控制器中实现
TPLAutocompleteViewControllerDelegate
协议。 - 创建一个
TPLAutocompleteViewController
实例,并将父视图控制器分配给代理属性。 - 使用
[self presentViewController...]
显示TPLAutocompleteViewController
。 - 在
didAutocompleteWithPlace
代理方法中处理用户的选项。 - 在
didAutocompleteWithPlace
、didFailAutocompleteWithError
和wasCancelled
委托方法中解散控制器。
#import "ViewController.h"
@import TPLPlaces;
@interface ViewController () <TPLAutocompleteViewControllerDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self makeUIButton];
}
// Add a button to the view.
-(void)makeUIButton {
UIButton * button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(0.0, 0.0, 300.0, 35.0);
button.center = self.view.center;
[button setTitle:@"Show autocomplete" forState:UIControlStateNormal];
[button addTarget:self action:NSSelectorFromString(@"autocompleteClicked") forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
// Present the autocomplete view controller when the button is pressed.
-(void)autocompleteClicked {
// Initialize the instance of TPLAutocompleteViewController
TPLAutocompleteViewController * controller = [[TPLAutocompleteViewController alloc] init];
// Assign the parent view controller as the delegate property.
controller.delegate = self;
// Assign search location coordinate
controller.locationCoordinate = CLLocationCoordinate2DMake(33.522324, 73.094098);
// Display the autocomplete view controller.
[self presentViewController:controller animated:YES completion:nil];
}
// Handle the user's selection.
-(void)viewController:(TPLAutocompleteViewController *)viewController didAutocompleteWithPlace:(Place *)place
{
// Do something with the selected place.
NSLog(@"place name %@", place.name);
NSLog(@"place type %@", place.type);
NSLog(@"place address %@", place.address);
}
// Called when a error occurred
- (void)viewController:(TPLAutocompleteViewController *)viewController didFailAutocompleteWithError:(NSError *)error
{
[self dismissViewControllerAnimated:YES completion:nil];
// TODO: handle the error.
NSLog(@"Error: %@", [error description]);
}
// User canceled autocomplete controller.
- (void)wasCancelled:(TPLAutocompleteViewController *)viewController
{
[self dismissViewControllerAnimated:YES completion:nil];
}
@end
。