My problem is this: Using alamofire, I need to pass coordinates to a function (longitude, latitude) , in a position variable. I got the data successfully, but I can't add it to the function. The function is called "showSomeMarkers" in MapViewController.swift. Thanks!
Code:
class MapViewController: UIViewController {
@IBOutlet var tableView: UITableView!
@IBOutlet var googleMapView: GMSMapView!
fileprivate var coordinates = [Model]()
@IBAction func dismissAction(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
func getValues() {
Networking.shared.getAllValues(value: "/kyiv/places") { coordinates in
self.coordinates = coordinates
self.tableView.reloadData()
}
}
func showMarker (position: CLLocationCoordinate2D) {
let marker = GMSMarker()
marker.position = position
marker.title = "Kiev"
marker.snippet = "Capital of Ukraine"
marker.map = googleMapView
}
//THIS FUNCTION
func showSomeMarkers () {
let position = CLLocationCoordinate2D(latitude: 50 , longitude: 30)
let marker = GMSMarker(position: position)
marker.title = "Some title"
marker.map = googleMapView
}
override func viewDidLoad() {
super.viewDidLoad()
let camera = GMSCameraPosition(latitude: 50.45, longitude: 30.52, zoom: 5.7)
googleMapView.camera = camera
self.showMarker(position: googleMapView.camera.target)
getValues()
showSomeMarkers()
}
}
extension MapViewController: UITableViewDelegate , UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
coordinates.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell" , for: indexPath) as! Cell
let value = coordinates[indexPath.row]
cell.configureValues(value: value)
print(value)
return cell
}
}
Model:
class Model: NSObject {
var name: String
var lat: Double
var lng: Double
init(with modelDictionary: [String: Any]) {
name = modelDictionary["name"] as? String ?? ""
lat = modelDictionary["lat"] as? Double ?? 0.0
lng = modelDictionary["lng"] as? Double ?? 0.0
}
}
Cell:
class Cell: UITableViewCell {
@IBOutlet var nameLabel: UILabel!
var latitude: Double?
var longitude: Double?
var name: String?
func configureValues(value: Model) {
nameLabel.text = value.name
latitude = value.lat
longitude = value.lng
}
}
Since I needed 1 data type string from Json for the table view, I configure values in cell. Please , help. Thanks!
change your function to
func showSomeMarkers (latitude: Double, longitude: Double) {
let position = CLLocationCoordinate2D(latitude: latitude , longitude: longitude)
//rest of code
}
and call it from the closure in getValues
func getValues() {
Networking.shared.getAllValues(value: "/kyiv/places") { coordinates in
self.coordinates = coordinates
for coordinate in self.coordinates {
showSomeMarkers(latitude: coordinate.lat, longitude: coordinate.lng)
}
self.tableView.reloadData()
}
}