2016-11-22 50 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 
    } 

}