2016-11-22 47 views
1

在我的雨燕iOS应用,我想利用用户将自动完成搜索画面,并让他们在上下文中寻找到自己的当前位置。显然,因为没有办法通过当前位置背景给它,这是不可能与谷歌地方信息自动填充。斯威夫特:谷歌将选择器,避免了地图UI

我的第二个选择是使用谷歌将选择器的搜索界面,因为当我开始将选择器集中在当前的位置,然后点击搜索,它会搜索在上下文中的地方与当前位置。

我的问题是,是否有可能直接让用户放置选取器的搜索屏幕,然后在抓取选取的地点信息后关闭选取器,避免选取地点选择器的主UI?

+0

你有什么试过的?您是否检查了[iOS的Pick Picker指南](https://developers.google.com/places/ios-api/placepicker)? – noogui

+0

是的,我已阅读指南。 – Kashif

回答

0

这在文档中有点令人困惑,但我想你想要的是使用GMSAutocompleteViewController而不是地方选择器。

下面的示例代码,链接到文档here

import UIKit 
import GooglePlaces 

class ViewController: UIViewController { 

    // Present the Autocomplete view controller when the button is pressed. 
    @IBAction func autocompleteClicked(_ sender: UIButton) { 
    let autocompleteController = GMSAutocompleteViewController() 
    autocompleteController.delegate = self 
    present(autocompleteController, animated: true, completion: nil) 
    } 
} 

extension ViewController: GMSAutocompleteViewControllerDelegate { 

    // Handle the user's selection. 
    func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) { 
    print("Place name: \(place.name)") 
    print("Place address: \(place.formattedAddress)") 
    print("Place attributions: \(place.attributions)") 
    dismiss(animated: true, completion: nil) 
    } 

    func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Error) { 
    // TODO: handle the error. 
    print("Error: ", error.localizedDescription) 
    } 

    // User canceled the operation. 
    func wasCancelled(_ viewController: GMSAutocompleteViewController) { 
    dismiss(animated: true, completion: nil) 
    } 

    // Turn the network activity indicator on and off again. 
    func didRequestAutocompletePredictions(_ viewController: GMSAutocompleteViewController) { 
    UIApplication.shared.isNetworkActivityIndicatorVisible = true 
    } 

    func didUpdateAutocompletePredictions(_ viewController: GMSAutocompleteViewController) { 
    UIApplication.shared.isNetworkActivityIndicatorVisible = false 
    } 

}