2015-08-21 67 views
2

在我的一個應用程序,我需要地理編碼地址字符串。起初我考慮使用CLGeocoder。然而,在我嘗試過之後,我偶然發現了一個我在this問題中描述的問題。alamofire與谷歌地理編碼api

解決方案是使用Google的Geocoding API。我現在已經切換到他們,並設法讓他們具有以下功能工作:

func startConnection(){ 
    self.data = NSMutableData() 
    let urlString = "https://maps.googleapis.com/maps/api/geocode/json?address=\(searchBar.text!)&key=MYKEY" 

    let linkUrl:NSURL = NSURL(string:urlString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!)! 
    let request: NSURLRequest = NSURLRequest(URL: linkUrl) 
    let connection: NSURLConnection = NSURLConnection(request: request, delegate: self, startImmediately: false)! 
     connection.start() 
} 

func connection(connection: NSURLConnection!, didReceiveData data: NSData!){ 
    self.data.appendData(data) 
} 

func connectionDidFinishLoading(connection: NSURLConnection!) { 
    do { 
     if let json = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as? [String: AnyObject] { 
      print(json) 
     } 
    } 
    catch { 
     print("error1") 
    } 
} 

這個偉大的工程,並解決它,我與CLGeocoder問題。但是,除了提取位置的座標之外,我還需要使用Google的Timezone API爲每個位置提取時區。

這樣做與NSURLConnectionNSURLSession在我看來有點困難,因爲我需要跟蹤哪個會話/連接返回。所以,我想要有一些使用完成處理程序的解決方案。

我試過使用Alamofire框架(使用正確的分支爲Swift 2.0)。但是,在這種情況下,似乎request()函數是錯誤的。我曾嘗試過:

let parameters = ["address":searchBar.text!,"key":"MYKEY"] 
Alamofire.request(.GET, "https://maps.googleapis.com/maps/api/geocode/json", parameters: parameters).responseJSON(options:.AllowFragments) { _, _, JSON in 
      print(JSON) 
     } 

而我打印的所有內容都是「SUCCESS」。我希望我做錯了什麼,它可以被修復,因爲我真的希望能夠使用閉包而不是委託調用。

我的問題是:

  1. 是否有可能使用Alamofire與谷歌地理編碼的API?
  2. 如果是這樣,你能告訴我我做錯了什麼?
  3. 如果這是不可能的,你可以請我建議我如何設計一個系統NSURSession s或NSURLConnection s這將允許我使用完成處理程序爲每個電話而不是代表?

P.S.我知道我可以使用同步請求,但我真的想避免使用該選項

更新

有人建議加入.MutableContainers作爲一個選項應該responseJSON工作。我試了下面的代碼:

let apiKey = "MYKEY" 
var parameters = ["key":apiKey,"components":"locality:\(searchBar.text!)"] 
Alamofire.request(.GET, "https://maps.googleapis.com/maps/api/geocode/json", parameters: parameters).responseJSON(options:.MutableContainers) { one, two, JSON in 
    print(JSON) 
} 

而我打印的是「成功」。

回答

-1

好的,我終於明白了這一點(藉助@cnoon的幫助)。返回的值是類型Result。我找不到它的文檔,但源代碼可用here

爲了檢索下面實施JSON可用於:

Alamofire.request(.GET, "https://mapss.googleapis.com/maps/api/geocode/json", parameters: parameters).responseJSON(options:.MutableContainers) { _, _, JSON in 
    switch JSON { 

    case .Failure(_, let error): 
     self.error = error 
     break 

    case .Success(let value): 
     print(value) 
     break 

    } 
} 

value印刷是從地理編碼的API響應的正確表示。

+1

這肯定會使用'responseJSON'。您只需將自定義閱讀選項關閉到序列化程序。它默認爲'。AllowFragments',你顯然需要'.MutableContainers'。 – cnoon

+0

@cnoon不幸的是它不工作。請參閱我的問題中的「更新」部分。另外,如果你能夠,你能看看這個問題,我有關於提交beta測試(這個問題肯定是由於框架)http://stackoverflow.com/questions/32158112/alamofire-swift-2 -cannot-submit-for-beta-testing-xcode-7-beta-5 –

+1

成功意味着它正在工作。在'swift-2.0'分支中,'responseJSON'現在返回一個Result類型,當打印時,將打印'SUCCESS'或'FAILURE'。如果您需要更多信息,請使用'debugPrint'。查看文檔中的「Result」類型和示例,瞭解如何從「Result」中獲取實際值。 – cnoon