Last active
August 29, 2015 14:20
-
-
Save zkkmin/ef6930209c004e8cbdd6 to your computer and use it in GitHub Desktop.
This file contains 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
// Call OneMap's Address Search API to search a location in Singapore | |
func getAddresses(keyWord: String ){ | |
if keyWord != "" { | |
let keyWordEscaped = keyWord.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding) | |
let urlString = "http://www.onemap.sg/API/services.svc/basicSearch?token=qo/s2TnSUmfLz+32CvLC4RMVkzEFYjxqyti1KhByvEacEdMWBpCuSSQ+IFRT84QjGPBCuz/cBom8PfSm3GjEsGc8PkdEEOEr&wc=SEARCHVAL%20LIKE%20%27\(keyWordEscaped!)$%27&otptFlds=CATEGORY&returnGeom=0&nohaxr=10" | |
session.dataTaskWithURL(NSURL(string: urlString)!, completionHandler: { | |
(taskData, taskResponse, taskError) -> Void in | |
var jsonReadError : NSError? | |
var jsonResult = NSJSONSerialization.JSONObjectWithData(taskData, options: NSJSONReadingOptions.MutableContainers, error: &jsonReadError) as NSDictionary | |
let resultsArray = jsonResult["SearchResults"] as NSArray | |
self.searchResults = [] | |
for (index, result) in enumerate(resultsArray){ | |
if index > 0 { | |
var place = Place() | |
if let placeName = result["SEARCHVAL"] as String? { | |
place.placeName = placeName | |
} | |
if let category = result["CATEGORY"] as String? { | |
place.Category = category | |
} | |
if let x = result["X"] as NSString? { | |
place.X = x.doubleValue | |
} | |
if let y = result["Y"] as NSString? { | |
place.Y = y.doubleValue | |
} | |
self.searchResults.append(place) | |
} | |
} | |
self.resultTableViewController.tableView.reloadData() | |
}).resume() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment