2017-02-18 11 views
0

私はオートコンプリートボックスを実装しようとしています。このアプリでは、tableviewの左にあるラベルを使って距離を表示したいと思います。私はPlaces Autocomplete APIを使用していると推測しますが、それはtableviewの左側に距離が表示されているかわかりません。google place autocomplete iOSで距離を表示する方法

This picture is what I want.

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

あなたは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) 
} 

これでc distanceを元に戻してフォーマットしてください。

CLLocationManagerを使用して現在のデバイスの場所を取得することもできます。

+0

ありがとうございました。今、私は距離を計算することができますが、私はTableviewのセルで左に距離を表示することはできません。 – DoBeBee

-1

ありがとうございました。今、私は距離を計算することができますが、私はTableviewのセルで左に距離を表示することはできません。

関連する問題