单击特定地址时,搜索栏中会出现Google地图错误

时间:2018-03-26 05:32:10

标签: swift google-maps

我在搜索栏中显示地址,但是当我点击单元格时出现错误。我在下面附上了代码。

  

错误:由于未捕获的异常'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()
    }

1 个答案:

答案 0 :(得分:1)

试试这个

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()
}