[英]Google maps error occurs in searchbar when clicked on particular address
我在搜索栏中显示地址,但某些地方当我单击单元格时发生错误。我在下面附加了代码。
错误:由于未捕获的异常“ NSRangeException”而终止应用程序,原因:“ ***-[__ NSSingleObjectArrayI objectAtIndex:]:索引1超出范围[0 .. 0]”
override func tableView(_ tableView: UITableView,
didSelectRowAt indexPath: IndexPath)
{
self.dismiss(animated: true, completion: nil)
let urlpath = "https://maps.googleapis.com/maps/api/geocode/json?address=\(self.searchResults[indexPath.row])&sensor=false".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
let url = URL(string: urlpath!)
let task = URLSession.shared.dataTask(with: url!) { (data, response, error) -> Void in
do {
if data != nil{
let dic = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! NSDictionary
let status = dic.value(forKey: "status") as! String
print("\(status)")
if status == "OK"
{
let lat = (((((dic.value(forKey: "results") as! NSArray).object(at: indexPath.row) as! NSDictionary).value(forKey: "geometry") as! NSDictionary).value(forKey: "location") as! NSDictionary).value(forKey: "lat")) as? Double
let lon = (((((dic.value(forKey: "results") as! NSArray).object(at: indexPath.row) as! NSDictionary).value(forKey: "geometry") as! NSDictionary).value(forKey: "location") as! NSDictionary).value(forKey: "lng")) as? Double
if let latitude = lat
{
if let longitude = lon
{
self.delegate.locateWithLongitude(longitude, andLatitude: latitude, andTitle: self.searchResults[indexPath.row])
}
}
}
}
}
catch
{
print("Error")
}
}
task.resume()
}
尝试这个
override func tableView(_ tableView: UITableView,
didSelectRowAt indexPath: IndexPath)
{
self.dismiss(animated: true, completion: nil)
let urlpath = "https://maps.googleapis.com/maps/api/geocode/json?address=\(self.searchResults[indexPath.row])&sensor=false".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
let url = URL(string: urlpath!)
let task = URLSession.shared.dataTask(with: url!) { (data, response, error) -> Void in
do {
if data != nil{
let dic = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! NSDictionary
let status = dic.value(forKey: "status") as! String
print("\(status)")
if status == "OK"
{
let lat = (((((dic.value(forKey: "results") as! NSArray).object(at: 0) as! NSDictionary).value(forKey: "geometry") as! NSDictionary).value(forKey: "location") as! NSDictionary).value(forKey: "lat")) as? Double
let lon = (((((dic.value(forKey: "results") as! NSArray).object(at: 0) as! NSDictionary).value(forKey: "geometry") as! NSDictionary).value(forKey: "location") as! NSDictionary).value(forKey: "lng")) as? Double
if let latitude = lat
{
if let longitude = lon
{
self.delegate.locateWithLongitude(longitude, andLatitude: latitude, andTitle: self.searchResults[indexPath.row])
}
}
}
}
}
catch
{
print("Error")
}
}
task.resume()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.