Skip to content

Instantly share code, notes, and snippets.

@ayman
Created August 17, 2015 18:33
Show Gist options
  • Select an option

  • Save ayman/bc3e1160fc1ba1d6eda1 to your computer and use it in GitHub Desktop.

Select an option

Save ayman/bc3e1160fc1ba1d6eda1 to your computer and use it in GitHub Desktop.
Simple Lookup to Bing for their ReverseGeocoder
class func bingReverseGeocode(inout location: CLLocation,
callback: (CLLocation) -> Void) {
var bingAPIKey = "ABCDEF-1234567890"
var lat = location.coordinate.latitude
var lon = location.coordinate.longitude
var u = "http://dev.virtualearth.net/REST/v1/Locations/\(lat),\(lon)?o=json&key=\(bingAPIKey)"
var url = NSURL(string: u)
var request: NSURLRequest = NSURLRequest(URL: url!)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(url!, completionHandler: {data, response, error -> Void in
if(error != nil) {
// If there is an error in the web request, print it to the console
println(error.localizedDescription)
}
var err: NSError?
// Uses SiftyJSON https://github.com/SwiftyJSON/SwiftyJSON
var j = JSON(data: data)
var place = j["resourceSets"][0]["resources"][0]["name"].stringValue
var address = j["resourceSets"][0]["resources"][0]["address"]["locality"].stringValue
var displayName = (address != "") ? address : ((place != "") ? place : "Unknown Location")
cluster.displayName = displayName
dispatch_async(dispatch_get_main_queue(), {
callback(cluster)
})
})
task.resume()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment