我正在尝试实现一个自动完成框。在该应用程序中,我想在tableview中显示左侧标签的距离。我认为他们正在使用Places Autocomplete API,但我不知道它在tableview的左侧显示距离。
extension ViewController: GMSAutocompleteResultsViewControllerDelegate {
func resultsController(_ resultsController: GMSAutocompleteResultsViewController,
didAutocompleteWith place: GMSPlace) {
searchController?.isActive = false
// Do something with the selected place.
print("Place name: \(place.name)")
print("Place address: \(place.formattedAddress)")
print("Place attributions: \(place.attributions)")
self.googleMapsView.clear()
let position: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: place.coordinate.latitude, longitude: place.coordinate.longitude)
let marker:GMSMarker = GMSMarker(position: position)
markerPosition = CLLocation(latitude: marker.position.latitude, longitude: marker.position.longitude)
marker.title = place.name
marker.appearAnimation = kGMSMarkerAnimationPop
marker.icon = GMSMarker.markerImage(with: .red)
marker.map = self.googleMapsView
self.googleMapsView.camera = GMSCameraPosition.camera(withLatitude: (place.coordinate.latitude), longitude: (place.coordinate.longitude), zoom: 17.0, bearing: 0, viewingAngle: 0)
searchController?.searchBar.text = place.name
self.dismiss(animated: true, completion: nil) // dismiss after select place
}
func resultsController(_ resultsController: GMSAutocompleteResultsViewController,
didFailAutocompleteWithError error: Error){
// TODO: handle the error.
print("Error: ", error.localizedDescription)
}
}
答案 0 :(得分:0)
您可以使用 cout << new char << endl;
对象上的函数计算给定GMSPlace
与当前设备位置之间的距离:
CLLocation
在你的具体例子中:
func distance(from location: CLLocation) -> CLLocationDistance
现在,您可以将if let currentPosition = self.googleMapsView.myLocation {
//returns the distance between two CLLocation objects in meters
let distance = markerPosition.distance(from: currentPosition)
}
转换并格式化为您想要的内容。
或者您可以使用CLLocationManager获取当前设备位置。
答案 1 :(得分:-1)
谢谢你的回答。现在,我可以计算距离,但我无法在Tableview单元格中显示左侧距离。