0

How can I reverse geocode locations in each tableview row cell?

I have a table view controller where each uber request comes in as latitude and longitude. How can I convert each coordinate to actual address in a tableviewcontroller?

The code below in cellForRowAt is:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    if self.rideRequests.count != 0 {

        let request = self.rideRequests[indexPath.row]

        if let cell = tableView.dequeueReusableCell(withIdentifier: "RequestsCell") as? RequestsCell {

            cell.configureCell(requests: request)


            return cell
        }
    }

    return RequestsCell()
}

Reverse Geocode location in my rider app (uber-clone) is:

func geoCode(location : CLLocation!) {
    /*  Only one reverse geocoding can be in progress at a time so we need to cancel existing
     one if we are getting location updates */

    geoCoder.cancelGeocode()
    geoCoder.reverseGeocodeLocation(location, completionHandler: { (data, error) -> Void in
        guard let placeMarks = data as [CLPlacemark]! else {
            return
        }
        let loc: CLPlacemark = placeMarks[0]
        let addressDict : [NSString: NSObject] = loc.addressDictionary as! [NSString: NSObject]
        let addrList = addressDict["FormattedAddressLines"] as! [String]
        let address = addrList.joined(separator: ", ")
        print(addrList)
        self.address.text = address
        self.previousAddress = address
    })
}

Configure cell from another file:

class RequestsCell: UITableViewCell {

@IBOutlet weak var longitudeLabel: UILabel!
@IBOutlet weak var latitudeLabel: UILabel!
@IBOutlet weak var usernameLabel: UILabel!

func configureCell(requests: Requests) {

    self.longitudeLabel.text = "\(requests.longitude)"
    self.latitudeLabel.text = "\(requests.latitude)"
    self.usernameLabel.text = "\(requests.usersname)"
}

} // class RequestsCell

LizG
  • 2,246
  • 1
  • 23
  • 38

1 Answers1

1

You should do this in:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{

}

method.

SNarula
  • 548
  • 3
  • 16
  • I added the code I have for cellForRowAt indexPath. Where in there can I add the reverse geocode? – LizG Dec 29 '16 at 12:22
  • You may be getting Latitide and Longitude from array. Get that value from particular index and then convert – SNarula Dec 29 '16 at 12:25
  • I added how I am getting the address in my rider app. When I try to put in the words geocode, I get an error stated it is undeclared. Also added my configure cell code as well. – LizG Dec 29 '16 at 12:27