GoongGeocoder 0.0.7

GoongGeocoder 0.0.7

ios.binhdev 维护。



GoongGeocoder

GoongGeocoder 使您的 iOS 应用程序连接到 Goong Geocoding API 和 Goong Autocomplete API变得更加容易。

入门

在您的 CocoaPods Podfile 中指定以下依赖项

pod 'GoongGeocoder'

然后 引入 GoongGeocoder@import GoongGeocoder;

对于 Objective-C 目标,可能需要启用 ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES 构建设置。

此存储库包含使用 Swift 和 Objective-C 编写的示例应用程序(包括两种语言编写应用的比较),并提供更多示例和详细文档,可在 Goong API 文档 中找到。

使用说明

您需要一个 Goong API 密钥 来使用此 API。如果您已经在使用 Goong Maps SDK for iOS,则 GoongGeocoder.swift 将自动识别您的访问令牌,前提是您已将其放在您的应用程序的 Info.plist 文件中的 GoongAccessToken 键中。

自动完成 UI

要使用 GoongAutocompleteViewController,只需显示它即可。

let vc = GoongAutocompleteViewController()
let nav = UINavigationController(rootViewController: vc)
vc.delegate = self
self.navigationController?.present(nav, animated: true, completion: nil)
GoongAutocompleteViewController *vc = [[GoongAutocompleteViewController alloc] init];
UINavigationController *nav = [UINavigationController alloc] initWithRootViewController:vc];
vc.delegate = self;
[self.navigationController presentViewController:nav animated:YES completion:nil];

实现 GoongAutocompleteDelegate,当用户在 tableView 中轻触位置时调用此委托方法。

public func viewController(_ viewController: GoongAutocompleteViewController, didAutocompleteWith place: Placemark?) {
   print(place?.name)
   print(place?.location)
}

处理错误

public func viewController(_ viewController: GoongAutocompleteViewController, didFailAutocompleteWithError error: Error?) {
   print(error?.localizedDescription)
}

基本

Swift 中的主地理编码类是 Geocoder,Objective-C 中的是 GoongGeocoder。使用您的访问令牌创建地理编码对象。

// main.swift
import GoongGeocoder

let geocoder = Geocoder(accessToken: "<#your access token#>")
// main.m
@import GoongGeocoder;

GoongGeocoder *geocoder = [[GoongGeocoder alloc] initWithAccessToken:@"<#your access token#>"];

或者,您可以将访问令牌放置在应用程序的 Info.plist 文件的 GoongAccessToken 键中,然后使用共享的地理编码对象。

// main.swift
let geocoder = Geocoder.shared
// main.m
GoongGeocoder *geocoder = [GoongGeocoder sharedGeocoder];

手头有了地理编码对象后,构建一个地理编码选项对象,并将其传递给 Geocoder.geocode(_:completionHandler:) 方法。

自动完成或正向地理编码

正向地理编码接收一个人类可读的查询,例如地点名称或地址,并产生任何与该查询对应的地理坐标。要执行正向地理编码,请使用 Swift 中的 ForwardGeocodeOptions 或 Objective-C 中的 GoongForwardGeocodeOptions。

// main.swift

let options = ForwardGeocodeOptions(query: "san bay noi bai")
options.focalLocation = CLLocation(latitude: 21, longitude: 105)
let task = geocoder.geocode(options) { (result, error) in
    guard let result = result else {
        return
    }
    print(result.predictions)
    // GeocodeResult provide predictions if you use ForwardGeocodeOptions
}
// main.m

GoongForwardGeocodeOptions *options = [[GoongForwardGeocodeOptions alloc] initWithQuery:@"san bay noi bai"];
options.focalLocation = [[CLLocation alloc] initWithLatitude:21 longitude:105];


NSURLSessionDataTask *task = [geocoder geocodeWithOptions:options
                                        completionHandler:^(GeocodeResult * _Nullable result,                                                            
                                                            NSError * _Nullable error) {
   // GeocodeResult provide predictions if you use GoongForwardGeocodeOptions
}];

反向地理编码

反向地理编码接收一个地理坐标并产生一组描述该坐标位置的地理位置分层,通常以地址开始。要执行反向地理编码,请使用 Swift 中的 ReverseGeocodeOptions 或 Objective-C 中的 GoongReverseGeocodeOptions。

// main.swift
let options = ReverseGeocodeOptions(coordinate: CLLocationCoordinate2D(latitude: 21.21760917728946, longitude: 105.7922871444448))
// Or perhaps: ReverseGeocodeOptions(location: locationManager.location)

let task = geocoder.geocode(options) { (result, error) in
    guard let result = result else {
        return
    }
    // GeocodeResult provide placemarks if you use ReverseGeocodeOptions
    print(result.placemarks.first)        
}
// main.m
GoongReverseGeocodeOptions *options = [[GoongReverseGeocodeOptions alloc] initWithCoordinate: CLLocationCoordinate2DMake(21.21760917728946, 105.7922871444448)];


NSURLSessionDataTask *task = [geocoder geocodeWithOptions:options
                                        completionHandler:^(GeocodeResult * _Nullable result,                                                            
                                                            NSError * _Nullable error) {
  
}];

地点详情

地点详情允许您根据其 ID 获取地点的详细信息。

geocoder.fetchPlace(from: <#Place ID#>) { (<#PlaceDetailResult?#>, <#NSError?#>) in
    <#code#>
}
[geocoder fetchPlace:@"" completionHandler:^(PlaceDetailResult * _Nullable result, NSError * _Nullable err) {
    <#code#>
}]