SPGooglePlacesAutocompleteRegion 1.0.4

SPGooglePlacesAutocompleteRegion 1.0.4

测试测试
语言语言 Obj-CObjective C
许可协议 MIT
发布最后发布2014年12月

Justin Howlett 维护。



  • Matej Bukovinski 和 Chris Chen

SPGooglePlacesAutocomplete

SPGooglePlacesAutocomplete 是围绕 Google Places Autocomplete API 的简单 objective-c 封装器。该 API 可以在用户输入文本时提供自动补全功能,通过返回诸如企业、地址和兴趣点等地点作为搜索结果。SPGooglePlacesAutocomplete 还提供将地点结果转换为 CLPlacemark 对象以方便与 MKMapView 进行映射的支持。

屏幕截图

如何使用

需求

SPGooglePlacesAutocomplete 需要 iOS 6.0 或更高版本的发布目标和 ARC。

安装

CocoaPods 是将 SPGooglePlacesAutocomplete 添加到项目的推荐方式。

  1. 在您的 Podfile 中添加 pod 项 pod 'SPGooglePlacesAutocomplete'
  2. 运行 pod installpod update
  3. 使用 #import "SPGooglePlacesAutocomplete.h" 在需要的位置包含 SPGooglePlacesAutocomplete。

执行查询

创建一个新的 SPGooglePlacesAutocompleteQuery 并填写您想指定的属性。

#import "SPGooglePlacesAutocomplete.h"

...

SPGooglePlacesAutocompleteQuery *query = [[SPGooglePlacesAutocompleteQuery alloc] initWithApiKey:@"YourGoogleAPIKey"];
query.input = @"185 berry str"; // search key word
query.location = CLLocationCoordinate2DMake(37.76999, -122.44696);  // user's current location
query.radius = 100.0;   // search addresses close to user
query.language = @"en"; // optional
query.types = SPPlaceTypeGeocode; // Only return geocoding (address) results.

请记住为浏览器应用程序提供 GoogleAPIKey,而不是 iOS 应用程序。

然后,调用 -fetchPlaces 来ping Google 的 API 并获取结果。返回的数组将返回 SPGooglePlacesAutocompletePlace 类的对象。

[query fetchPlaces:^(NSArray *places, NSError *error) {
    NSLog(@"Places returned %@", places);
}];

如果您需要更新查询(例如,当用户键入时),只需更新适当的属性,然后再次调用 -fetchPlaces。任何挂起的请求将自动取消,并发出具有更新属性的新请求。

将地点解析为 CLPlacemarks

Google Places Autocomplete API 将返回与您的查询匹配的地点名称。但是,它不会返回这些结果的相关经纬度信息。SPGooglePlacesAutocomplete 通过解析地点结果为 placemarks 来处理这种情况。只需在 SPGooglePlacesAutocompletePlace 上调用 -resolveToPlacemark 即可。

[query fetchPlaces:^(NSArray *places, NSError *error) {
    SPGooglePlacesAutocompletePlace *place = [places firstObject];
    if (place) {
        [place resolveToPlacemark:^(CLPlacemark *placemark, NSString *addressString, NSError *error) {
            NSLog(@"Placemark: %@", placemark);
        }];
    }
}];

当搜索“地理编码”(地址)地点时,库使用CLGeocoder来进行地址地理编码。当搜索“场所”(商业)地点时,库将自动ping 谷歌地点API 来获取地理定位业务所需的详细信息。

示例代码

有关如何使用SPGooglePlacesAutocomplete的示例,请参阅附带示例项目。我在项目中放置了一个功能性Google API密钥,请勿在其它地方使用它!