FTGooglePlacesAPI 1.1

FTGooglePlacesAPI 1.1

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布日期上次发布2014年12月

Lukas Kukacka维护。




  • Fuerte International 和 Lukas Kukacka

一个开源的 iOS Objective C 库,用于通过基于简单回调块的界面查询 Google Places API。

通过几行代码轻松执行 Google Places API 请求。库包括但不限于以下内容:

  • 地点搜索
    • 附近搜索(在指定区域内搜索地点)
    • 文本搜索(根据搜索字符串搜索地点)
  • 地点详情(获取有关地点的更综合信息)
FTGooglePlacesAPI example screenshot FTGooglePlacesAPI example screenshot FTGooglePlacesAPI example screenshot

安装

手动(或使用 git 子模块)

  1. 集成 AFNetworking 2.0
    • FTGooglePlacesAPI 使用 AFNetworking 2.0 进行所有网络请求。为什么?因为它酷!
  2. 从本仓库下载源文件
    • 或使用 GIT 子模块(git submodule add [email protected]:FuerteInternational/FTGooglePlacesAPI.git
  3. FTGooglePlacesAPI 文件夹中的所有文件添加到您的项目的目标中

使用方法

您可以查看详细的示例使用项目 XcodeProject/FTGooglePlacesAPI.xcodeproj。只需确保提供自己的 API 密钥。或者按照以下步骤自己开始...

1. 在实现文件中导入 FTGooglePlacesAPI 文件

#import "FTGooglePlacesAPI.h"

2. 设置服务

为了与 Google Places API 进行通信,您必须首先生成自己的 API 密钥,您可以在 Google Play 开发者控制台 获取。您还可以查看 介绍 - Google Places API

在您使用它发出任何请求之前,您必须向 FTGooglePlacesAPI 服务提供 API 密钥。将这些代码放在 App Delegate 中是一个好地方。

//  Provide API key to FTGooglePlacesAPIService before making any requests
[FTGooglePlacesAPIService provideAPIKey:@"YOUR_API_KEY"];

可选,您可以启用调试日志。如果将其设置为 YES 并在调试模式下构建(#ifdef DEBUG),则服务将打印一些调试信息到控制台。实际上,即使在发布构建中也不需要删除此代码,因为在非调试构建中,服务不会产生任何输出。

//  Optionally enable debug mode
[FTGooglePlacesAPIService setDebugLoggingEnabled:YES];

3. 创建一个请求

构造一个所需的请求。

//  Create location around which to search (hardcoded location of Big Ben here)
CLLocationCoordinate2D locationCoordinate = CLLocationCoordinate2DMake(51.501103,-0.124565);

//  Create request searching nearest galleries and museums
FTGooglePlacesAPINearbySearchRequest *request = [[FTGooglePlacesAPINearbySearchRequest alloc] initWithLocationCoordinate:locationCoordinate];
request.rankBy = FTGooglePlacesAPIRequestParamRankByDistance;
request.types = @[@"art_gallery", @"museum"];

由于实现的请求对象支持API的所有功能,因此有很多可能性。请参阅示例项目和Google Places API文档

提示:您可以使用FTLocationManager非常容易地确定用户的当前位置。

4. 执行请求并处理结果

//  Execute Google Places API request using FTGooglePlacesAPIService
[FTGooglePlacesAPIService executeSearchRequest:request
                         withCompletionHandler:^(FTGooglePlacesAPISearchResponse *response, NSError *error) {

     //  If error is not nil, request failed and you should handle the error
     if (error)
     {
         // Handle error here
         NSLog(@"Request failed. Error: %@", error);

         //  There may be a lot of causes for an error (for example networking error).
         //  If the network communication with Google Places API was successfull,
         //  but the API returned some non-ok status code, NSError will have
         //  FTGooglePlacesAPIErrorDomain domain and status code from
         //  FTGooglePlacesAPIResponseStatus enum
         //  You can inspect error's domain and status code for more detailed info
     }

     //  Everything went fine, we have response object we can process
     NSLog(@"Request succeeded. Response: %@", response);
}];

您必须根据要执行请求的类型使用正确的方法,因为服务将根据调用的方法构造响应对象。

可用的方法有:

  • + (void)executeSearchRequest:withCompletionHandler:用于执行最近位置文本搜索请求
  • + (void)executeDetailRequest:withCompletionHandler:用于执行地点详情请求

兼容性

  • iOS 6+
    • 主要因为依赖于AFNetworking 2.0(虽然您很难去除对它的依赖)
  • ARC

联系方式

FTGooglePlacesAPI由Fuerte International开发。请发送邮件给我们,告知我们您如何使用此组件。

贡献和备注

  • 如果您喜欢这个库,请考虑给它一个 Github star,让我们知道。
  • 所有头文件都经过大量注释,格式与Xcode 5快速文档预览(Option + Click)兼容
  • 库使用Apple的XCTestOCMock进行单元测试

欢迎提出拉取请求,但需要您遵循一些规则:

  • 在代码注释和Git提交消息中记录您的更改
  • 确保您的更改没有通过包含的示例项目、单元测试以及必要时实施针对新添加的功能的单元测试和示例代码造成任何问题

版本历史

1.1

  • 实现了对使用idreference属性的弃用,因为这些属性已被Google弃用,时间从2014年6月24日起。有关更多信息,请参阅文档中的弃用通告

1.0

  • 首次正式公开发布

许可证

The MIT License (MIT)

Copyright (c) 2013-2014 Fuerte International

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.