###前提・実現したいこと
alamofireでapiを叩いて情報を格納したいです。
現在地を取得してそれをパラメータにapiを叩くアプリを作っているのですが、breakpointで見たところ、apiのリクエストをalamofireがした後に下記コードのlet latitude = "(myLocationManager.location?.coordinate.latitude)"という行に戻って来てしまいます。
原因はなんなのでしょうか、、、、
よろしくお願いいたします。
また不足している情報等がございましたら、お申し付けください。
###発生している問題・エラーメッセージ
nilが返り続けてしまいます。
エラーメッセージ
###該当のソースコード
swift3
ここにご自身が実行したソースコードを書いてください
import UIKit
import CoreLocation
import Alamofire
import SwiftyJSON
class TopViewController: UIViewController,CLLocationManagerDelegate,UITableViewDelegate,UITableViewDataSource {
let myLocationManager:CLLocationManager = CLLocationManager() var libraries: [Library] = [] @IBOutlet weak var table: UITableView! //---------------------------------現在地------------------------------------------------- override func viewDidLoad() { super.viewDidLoad() myLocationManager.delegate = self myLocationManager.startUpdatingLocation() // Do any additional setup after loading the view, typically from a nib. } func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { //if let location = manager.location{ //prepare(for: "showDetail", sender: self) //let detailvc = segue.destina //print(location.coordinate.latitude) //} let latitude = "\(myLocationManager.location?.coordinate.latitude)" let longitude = "\(myLocationManager.location?.coordinate.longitude)" myLocationManager.stopUpdatingLocation() let parameters:Parameters = [ "appkey":"9500e8c7c8591a8cd5171f2d9965720a", "geocode":"\(longitude),\(latitude)", "limit":"10", "format":"json", "callback":"" ] Alamofire.request("https://api.calil.jp/library", parameters:parameters,encoding:URLEncoding.default).responseJSON { response in print(response.result.value) guard let object = response.result.value else{ return } let json = JSON(object) print(self.libraries) for json3 in json.arrayValue { let library: Library = Library(fromJson: json3) self.libraries.append(library) } self.table.reloadData() print(self.libraries) } table.dataSource = self table.delegate = self } func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) { print("error") }
###試したこと
緯度と経度は取れていました。
###補足情報(言語/FW/ツール等のバージョンなど)
ライブラリ:alamofire,swiftyjson,corelocation
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。