2017-09-04 101 views
0

我想在Swift 3.0中使用谷歌地方做Google自動完成。但我需要根據當前位置進行搜索。例如,如果我在印度加爾各答,然後鍵入搜索關鍵字「Ko」,它將顯示加爾各答第一個結果使用本地搜索快速谷歌自動完成

任何人都可以幫助我。 這是我的代碼。 我進口GooglePlaces在我的課

@IBAction func txtFieldLocationDidStartEditing(_ sender: Any) { 
    self.placeAutocomplete() 
} 
func placeAutocomplete() { 
    let autocompleteController = GMSAutocompleteViewController() 
     autocompleteController.delegate = self 
    present(autocompleteController, animated: true, completion: nil) 
} 

// MARK: - 自動完成委託

func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) { 
    print("Place name: \(place.name)") 
    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 
} 

請人幫我解決一下。 在此先感謝。

+0

你可以與你的MapView的邊框 – nathan

回答

1

GMSAutocompleteViewController提供的唯一的API就是設置GMSCoordinateBounds像這樣(reference):

func placeAutocomplete() { 
    let visibleRegion = mapView.projection.visibleRegion() 
    let bounds = GMSCoordinateBounds(coordinate: visibleRegion.farLeft, coordinate: visibleRegion.nearRight) 

    let autocompleteController = GMSAutocompleteViewController() 
    acController.autocompleteBounds = bounds 
    autocompleteController.delegate = self 
    present(autocompleteController, animated: true, completion: nil) 
} 
+0

提供API謝謝@nathan,設置範圍是工作的罰款。 –

相關問題