2017-02-26 2 views
0

地図ビュー上にポップオーバー画面を表示するXcodeでアプリを作成しています。現在の場所に基づいて近くの場所を検索します。私はこれを行ういくつかの方法を試みたが、これを達成していない。私は比較的新しいコーディングをしています.Googleプレイスのドキュメントはあまりにもあまりにも漠然としています。Google Places APIを使用して、XcodeのUITableViewにユーザーの現在地付近の場所のリストを表示します。

私はすでにGoogleプレイス(ダウンロード済み、リンク済み、APIキーなど)を実装していると言われています。リストビューで近くの場所を表示する正しい方法を見つけようとしています。私はSwiftを使ってこれを作成しています。

import UIKit 
import GooglePlaces 

class ViewController: UIViewController, UITableViewDataSource { 

    var placesClient: GMSPlacesClient! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     self.nearbyPlaces() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    func nearbyPlaces() { 
     placesClient.currentPlace(callback: { (placeLikelihoodList, error) -> Void in 
      if let error = error { 
       print("Pick Place error: \(error.localizedDescription)") 
       return 
      } 

      if let placeLikelihoodList = placeLikelihoodList { 
       for likelihood in placeLikelihoodList.likelihoods { 
        let place = likelihood.place 
       } 
      } 
     }) 
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return GMSPlaceLikelihoodList.count 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     var cell = UITableViewCell() 
     cell.textLabel?.text = "Test" 
     return cell 
    } 
} 

答えて

1

あなたが実際にソリューションに近い、

  1. は、結果を保持するための変数likeHoodListを作成してテーブルビューの参照のための出口
  2. を作成します:それはめちゃくちゃですので、もしここでは、私がこれまで持っているものですGMSPlacesClient リクエスト。
  3. GMSPlacesClientコールバックを実行して結果をlikeHoodListおよび リロードテーブルビューに保存した場合。
  4. cellForRowAtに場所名などを書き込むことを忘れないでください。 個の場所の数は、あなたのテーブルビューの行数です。
  5. コールバックパラメータplaceLikelihoodListは GMSPlaceLikelihoodListの参照です。
  6. GMSPlaceLikelihoodListは、GMSPlaceLikelihood
  7. GMSPlaceLikelihoodのアレイは、2つの特性、場所及び尤度
  8. 場所名などその 場所についての特性を有するGMSPlace基準であるを有する2つの特性、尤度と 帰属
  9. 尤度を有しますplacaeID、ここ等

座標は

、完全なソリューションであります
import UIKit 
import GooglePlaces 

class ViewController: UIViewController, UITableViewDataSource { 

    var placesClient: GMSPlacesClient! 
    var likeHoodList: GMSPlaceLikelihoodList? 
    @IBOutlet var tableView: UITableView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     self.nearbyPlaces() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    func nearbyPlaces() { 
     placesClient.currentPlace(callback: { (placeLikelihoodList, error) -> Void in 
      if let error = error { 
       print("Pick Place error: \(error.localizedDescription)") 
       return 
      } 

      if let placeLikelihoodList = placeLikelihoodList { 
       likeHoodList = placeLikelihoodList 
       tableView.reloadData() 
      } 
     }) 
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     if let likeHoodList = likeHoodList { 
      return likeHoodList.likehoods.count 
     } 
     return 0 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     var cell = UITableViewCell() 
     var place = likeHoodList.likelihoods[indextPath.row].place //this is a GMSPlace object 
     //https://developers.google.com/places/ios-api/reference/interface_g_m_s_place 
     cell.textLabel?.text = place.name 
     return cell 
    } 
} 

更新

あなたが、デリゲートを初期化このようのviewDidLoad変更していないようだ、

override func viewDidLoad() { 
     super.viewDidLoad() 
     tableView.dataSource = self 
     self.nearbyPlaces() 
    } 
+0

ありがとう!これは素晴らしい!私が抱えている唯一の問題は、テーブルに "place.name"値を設定することです。エラーを返しています:タイプ 'GMSPlaceLikelihood?'の値メンバ 'name'がありません –

+0

@ M.Spencer、はい、それは私の悪いです、それは 'var place = likeHoodList.likelihoods [indextPath.row] .place'でなければなりません。 – ocanal

+0

素晴らしい応答!残念ながら、それはまだ人口が多いようには見えません...私は何もエラーはありませんが、リストはまだ空です、私はそれが愚かな間違いだと確信しています –

関連する問題