【发布时间】:2016-02-15 21:51:05
【问题描述】:
如果还有其他可能与该问题相关的内容,请告诉我,以便我尝试提供更多详细信息。我大约一个星期,我似乎无法找到问题所在。这可能会让我想把电脑扔在墙上,但我似乎无法弄清楚:(
该问题似乎已与坐标变量隔离,但该问题仍可能与类的构造方式有关,但我无法确定它们的构造方式。
-
我能够在发出请求之前将当前位置坐标打印到屏幕上,但在调用将发送 api 请求的方法时,我在 JSON 中收到以下错误
李>
“状态”:“INVALID_REQUEST”,
Google 文档声称:
“INVALID_REQUEST 通常表示缺少必需的查询参数(位置或半径)。”
我正在经历的是:
使用硬编码坐标,我可以进行搜索并成功返回 JSON。
我能够成功获取当前位置坐标。
但是;我无法将当前位置坐标插入到搜索查询中。
这是一个很好的代码块,我相信它是理解问题所需要的全部。我感谢任何可能导致解决问题的建议:
// all classes have their own swift file
class ViewController: UIViewController, CLLocationManagerDelegate
{
let currentLocation = CurrentLocation()
let downloadData = DownLoadData()
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>)
{
if !didFindMyLocation
{
let myLocation: CLLocation = change![NSKeyValueChangeNewKey] as! CLLocation
currentLocation.coordinate = myLocation.coordinate
currentLocation.latitude = myLocation.coordinate.latitude
currentLocation.longitude = myLocation.coordinate.longitude
didFindMyLocation = true
print(currentLocation.coordinates) // prints coordinates correctly
downLoadData.downloadAllLocations({ () -> () in //request I am attempting to make that outputs,
//"status" : "INVALID_REQUEST",
})
}
}
class CurrentLocation: NSObject, CLLocationManagerDelegate
{
override init()
{
super.init()
}
var locationManager = CLLocationManager()
//THE CULPRITS!
var latitude : CLLocationDegrees!
var longitude : CLLocationDegrees!
}
// typealias in a swift file of its own.
import foundation
typealias DownLoadComplete = () -> ()
class DownloadData: NSObject
{
let currentLocation = CurrentLocation()
override init()
{
super.init()
}
func downloadAllLocations(completed: DownloadComplete)
{
//hardcoded coordinates output correct JSON Response. So Why Cant I Interpolate the coordinates from my current location???
let URL_Search = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?"
let urlString = "\(URL_Search)location=\(currentLocation.latitude),\(currentLocation.longitude)&radius=\(searchRadius)&types=\(searchType)&key=\(API_ServerKey)"
let url = NSURL(string: urlString)!
print(url) // outputs nil,nil in the currentLocation.latitude and currentLocation.longitude spots
Alamofire.request(.GET,url).responseJSON { (response) -> Void in
switch response.result
{
case .Success:
if let value = response.result.value
{
let json = JSON(value)
print(json) // If hardcoded coordinates are inserted then I the json is outputted. If current location is attempted then I receive an INVALID_REQUEST error.
if let results = json["results"].array
{
for result in results {
if let places = result["place"].string
{
self.places = place
}
}
}
}
case .Failure(let error):
print(error)
}
completed()
}
}
}
在我发送请求之前通过 print(currentLocation.coordinates) 测试输出(downLoadData.downLoadAllLocation[调用了 print(url)])输出到调试控制台是:
CLLocationCoordinate2D(纬度:37.33233141,经度:-122.0312186) https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=nil,nil ...等
这里的问题是插入到搜索查询中的坐标显示为 nil,nil?我试过操纵代码。但是没有运气......它们被打印出来似乎很奇怪,但是在搜索中它们显示为 nil。这个怎么可能??
【问题讨论】:
-
你要构建什么
-
传递给 url 的硬编码坐标和其他值是什么?查询 str 构造中使用的任何变量是可选的吗?如果是这样,那么它可能会作为 Optional("...") 进入查询 url,因此您可能会收到无效请求。
-
尝试打印搜索查询 url,看看传递给 GET 调用的实际 url 是什么?
-
所以我想这适用于原始问题。如果我能够在发送请求并将正确的坐标输出到调试控制台之前实际打印(currentLocation.latitude)和打印(currentLocation.longitude),为什么它们为零。
-
这很有趣,因为我正在将我的所有代码从一个 Massive View Controller 转换为类。当它是一个 MVC 时,它运行良好。现在它不............所以我认为它必须对课程做一些事情,但我无法找到关于这个问题的任何信息。
标签: ios json swift google-maps google-places-api