【问题标题】:how to show distance in google place autocomplete iOS如何在谷歌地方自动完成iOS中显示距离
【发布时间】:2017-07-08 01:51:00
【问题描述】:

我正在尝试实现一个自动完成框。在那个应用程序中,我想在表格视图的左侧用标签显示距离。我认为他们正在使用 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)
    }
}

【问题讨论】:

    标签: jquery ios uitableview autocomplete google-places-api


    【解决方案1】:

    您可以使用CLLocation 对象上的函数计算给定GMSPlace 与您当前设备位置之间的距离:

    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)
    }
    

    现在您可以将distance 转换为您想要的任何格式。

    或者您可以使用CLLocationManager 获取当前设备位置。

    【讨论】:

    • 感谢您的回答。现在,我可以计算距离,但无法在 Tableview 单元格的左侧显示距离。
    【解决方案2】:

    谢谢你的回答。现在,我可以计算距离,但无法在表格视图单元格的左侧显示距离。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-03
      • 1970-01-01
      • 2014-05-21
      • 2020-02-27
      • 2017-07-09
      • 1970-01-01
      • 2018-08-25
      • 1970-01-01
      相关资源
      最近更新 更多