Created
September 13, 2014 15:36
-
-
Save pocketkk/1e4fed488dc6a57974aa to your computer and use it in GitHub Desktop.
Swift - Retrieve Lat and Long from Geocode.us
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import XCPlayground | |
XCPSetExecutionShouldContinueIndefinitely(continueIndefinitely: true) | |
class Geocode { | |
var url : NSURL! | |
var request : NSURLRequest! | |
let queue : NSOperationQueue = NSOperationQueue() | |
var address : String! | |
var responseString : String? | |
init(address: String) { | |
self.address = address | |
url = NSURL(string: processAddress(address)) | |
request = NSURLRequest(URL: url) | |
} | |
func processAddress(address: String) -> String { | |
var tempArray = splitString(address, by: " ") | |
var tempString = "http://rpc.geocoder.us/service/namedcsv?address=" | |
for (var i = 0; i<=tempArray.count-1 ; i++) { | |
if i < tempArray.count-1 { | |
tempString = tempString + tempArray[i] + "+" | |
} else { | |
tempString = tempString + tempArray[i] | |
} | |
} | |
return tempString | |
} | |
func setString(s: String) { | |
responseString = s | |
} | |
func splitString(s: String, by: String) -> [String] { | |
return s.componentsSeparatedByString(by) | |
} | |
func unZipString(arr: [String]) -> [String: String] { | |
var dict = [String: String]() | |
var reply = splitString(arr[0], by: "=") | |
dict[reply[0]] = reply[1] | |
var reply2 = splitString(arr[1], by: "=") | |
dict[reply2[0]] = reply2[1] | |
return dict | |
} | |
func doWorkOnResponse() { | |
var splitResponse = splitString(self.responseString!, by: ",") | |
var data = unZipString(splitResponse) | |
println("******************************") | |
println(data) | |
} | |
func getLatandLong() { | |
let task = NSURLSession.sharedSession().dataTaskWithURL(url) {(data, response, error) in | |
self.setString(NSString(data: data, encoding: NSUTF8StringEncoding)) | |
self.doWorkOnResponse() | |
} | |
task.resume() | |
} | |
} | |
var h = Geocode(address: "1350 14th st, Oakland, Ca 94607") | |
h.getLatandLong() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment