SearchControllerを使用して場所を検索しているときにSwift4を使用してプロジェクトにGoogleマップを統合し、URLを押してその場所への座標を取得し、一部のアドレスにnil応答を取得し、一部のアドレスが正しい応答を返します。なぜ起こっているのかわかりません。この問題について知っている人がいたら助けてください。前もって感謝します
これは、tableViewのdidSelectMethodでアドレスへの座標を(文字列形式で)取得するために使用するコードです。
override func tableView(_ tableView: UITableView,
didSelectRowAt indexPath: IndexPath){
// 1
self.dismiss(animated: true, completion: nil)
// 2
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!)
print(url!)
let task = URLSession.shared.dataTask(with: url! as URL) { (data, response, error) -> Void in
// 3
print(data!)
print(response!)
do {
if data != nil{
let dic = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableLeaves) as! NSDictionary
let result = dic.value(forKey: "results") as! NSArray
print(result)
if result.count != 0 {
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
// 4
self.delegate.locateWithLongitude(lon, andLatitude: lat, andTitle: self.searchResults[indexPath.row])
}else{
print("Data Not Found")
}
}
}catch {
print("Error")
}
}
// 5
task.resume()
}
リクエストにAPIキーがないようです。APIキーは必須になり、Googleはキーレスの使用を廃止します。現在、APIキーのないリクエストは一定期間ブロックされますが、将来的にはキーレスアクセスが完全にブロックされます。
6月11日以降、コア製品にアクセスするには、有効なAPIキーとGoogle CloudPlatformの課金アカウントが必要になります。
ソース:https://mapsplatform.googleblog.com/2018/05/introducing-google-maps-platform.html
ドキュメントに従ってAPIキーを作成し、httpsリクエストで使用する必要があります。
https://developers.google.com/maps/documentation/geocoding/get-api-key
また、WebサービスのAPIキーはIPアドレスの制限のみをサポートしているため、APIキーを保護し、応答をiOSアプリに返すために、中間サーバーからWebサービスリクエストを送信する必要があることにも注意してください。
APIキーの制限に関する次のドキュメントをご覧ください
https://developers.google.com/maps/faq#keysystem
これがお役に立てば幸いです。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加