我需要在Google地图中添加新位置。我知道我需要创建一个请求,其中包含有关JSON格式的新地点的信息,我需要的是如何传递请求。 提前谢谢。
var dictionaryObject: NSDictionary = [
"location": [
"lat": "\(latitude)",
"lng": "\(longitude)"
],
"accuracy": 50,
"name": "\(nameField.text)",
"phone_number": "\(phoneNoField.text)",
"address": "\(addressField.text)",
"types": "\(typeField.text)",
"website": "\(websiteField.text)",
"language": "en-AU"
]
var url: NSURL = NSURL(string: "https://maps.googleapis.com/maps/api/place/add/json?key=API-KEY")!
var request:NSMutableURLRequest = NSMutableURLRequest(URL:url)
request.HTTPMethod = "POST"
var error: NSError?
var theJSONData = NSJSONSerialization.dataWithJSONObject(
dictionaryObject ,
options: NSJSONWritingOptions(0),
error: nil)
var theJSONText = NSString(data: theJSONData!,
encoding: NSASCIIStringEncoding)
println((theJSONData))
//request.HTTPBody = theJSONText?.dataUsingEncoding(NSUTF8StringEncoding)
request.HTTPBody = theJSONData
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue())
{
(response, data, error) in
var parseError: NSError?
var responseObject = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: &parseError) as? NSDictionary
println((responseObject))
}
答案 0 :(得分:0)
最简单的方法是使用Google Places API for iOS中的addPlace
方法。请参阅Google Developers网站上的Place Add guide,其中包含Swift和Objective-C中的代码示例。
答案 1 :(得分:0)
var dictionaryObject: NSDictionary = [
"location": [
"lat": latitude,
"lng": longitude
],
"accuracy": 50,
"name": "\(nameField.text)",
"phone_number": "\(phoneNoField.text)",
"address": "\(addressField.text)",
"types": ["\(typeField.text)"],
"website": "\(websiteField.text)",
"language": "en-AU"
]
var url: NSURL = NSURL(string: "https://maps.googleapis.com/maps/api/place/add/json?key=API-KEY")!
var request:NSMutableURLRequest = NSMutableURLRequest(URL:url)
request.HTTPMethod = "POST"
var error: NSError?
var theJSONData = NSJSONSerialization.dataWithJSONObject(
dictionaryObject ,
options: NSJSONWritingOptions(0),
error: nil)
var theJSONText = NSString(data: theJSONData!,
encoding: NSASCIIStringEncoding)
println((theJSONData))
//request.HTTPBody = theJSONText?.dataUsingEncoding(NSUTF8StringEncoding)
request.HTTPBody = theJSONData
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue())
{
(response, data, error) in
var parseError: NSError?
var responseObject = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: &parseError) as? NSDictionary
println((responseObject))
}