我在Xcode中创建一个应用程序,该应用程序在地图视图上有一个弹出屏幕,允许用户根据当前位置从附近位置列表中进行选择。我已经尝试了几种方法来做到这一点,但还没有完成。我对编码比较陌生,Google Places文档对我来说有点太模糊了。
话虽如此,我已经实施了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
}
}
答案 0 :(得分:2)
你实际上接近解决方案,
likeHoodList
请求。likeHoodList
和。{
重新加载tableview。cellForRowAt
来写地名等
地方数是你的tableview行数。这是一个完整的解决方案,
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()
}