一个开源的 iOS Objective C 库,用于通过基于简单回调块的界面查询 Google Places API。
通过几行代码轻松执行 Google Places API 请求。库包括但不限于以下内容:
|
|
|
git submodule add [email protected]:FuerteInternational/FTGooglePlacesAPI.git
)您可以查看详细的示例使用项目 XcodeProject/FTGooglePlacesAPI.xcodeproj。只需确保提供自己的 API 密钥。或者按照以下步骤自己开始...
#import "FTGooglePlacesAPI.h"
为了与 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];
构造一个所需的请求。
// 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非常容易地确定用户的当前位置。
// 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:
用于执行地点详情请求FTGooglePlacesAPI由Fuerte International开发。请发送邮件给我们,告知我们您如何使用此组件。
欢迎提出拉取请求,但需要您遵循一些规则:
id
和reference
属性的弃用,因为这些属性已被Google弃用,时间从2014年6月24日起。有关更多信息,请参阅文档中的弃用通告。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.