我尝试使用 currentPlaceWithCallback 生成地点的可能性列表,效果很好。但是,在用户开始输入搜索之前,我很难找到一种方法来将从该可能性列表收集的信息插入到 GMSAutocompleteViewController / GMSAutocompleteResultsViewController 的视图中(此功能工作正常)。
任何建议将不胜感激!
func generateLikelihoodListViaPlacesClient() {
placesClient.currentPlaceWithCallback { (likelihoodlist, error) -> Void in
if error != nil {
println("Current Place error: \(error!.localizedDescription)")
return
}
for likelihood in likelihoodlist!.likelihoods {
let nearestPlace = likelihoodlist!.likelihoods.first
println(nearestPlace)
if let likelihood = likelihood as? GMSPlaceLikelihood {
let place = likelihood.place
self.placesArray!.insert(place.name, atIndex: 0)
println("Current Place name \(place.name) at likelihood \(likelihood.likelihood)")
println("Current Place address \(place.formattedAddress)")
println("Current Place attributions \(place.attributions)")
println("Current PlaceID \(place.placeID)")
}
}
}
}
我当前的 GMSAutocompleteViewController 在单击 UITextField 并触发 EditingDidBegin 文本字段委托函数后出现。我已经使用之前已经使用过的位置管理器设置了界限:
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let newLocation = locations.last {
println("#######################")
println("\(newLocation.coordinate)")
println("#######################")
// Creates northeast and southwest coordinate bounds for rectangle that autocompleteController returns results in
let currentCoordinate2D : CLLocationCoordinate2D = newLocation.coordinate
let northEastCoordinate2DWithBearing : CLLocationCoordinate2D = locationWithBearing(270, distanceMeters: 500, origin: currentCoordinate2D)
let southWestCoordinate2DWithBearing : CLLocationCoordinate2D = locationWithBearing(90, distanceMeters: 500, origin: currentCoordinate2D)
let bounds : GMSCoordinateBounds = GMSCoordinateBounds(coordinate: northEastCoordinate2DWithBearing, coordinate: southWestCoordinate2DWithBearing)
autocompleteController.autocompleteBounds = bounds
// Restricts filter to only businesses
let filter : GMSAutocompleteFilter = GMSAutocompleteFilter()
filter.type = .Establishment
autocompleteController.autocompleteFilter = filter
if #available(iOS 9.0, *) {
} else {
// Fallback on earlier versions
locationManager.stopUpdatingLocation()
}
}
}