Created
August 17, 2015 18:33
-
-
Save ayman/bc3e1160fc1ba1d6eda1 to your computer and use it in GitHub Desktop.
Simple Lookup to Bing for their ReverseGeocoder
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
| 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