使用新版Google Places Swift SDK for iOS时持续触发内部错误
解决Google Places Swift SDK iOS版的Internal Error(错误码2)问题
核心问题:坐标区域参数错误
你的代码中创建RectangularCoordinateRegion时,东北边界的纬度值小于西南边界,违反了地理区域的基本规则(东北方向必须纬度更高、西经地区经度为负数),这是触发内部错误的直接原因:
// 错误的坐标配置 northEast: CLLocationCoordinate2D(latitude: 20, longitude: 30), southWest: CLLocationCoordinate2D(latitude: 40, longitude: 50)
纽约市的正确大致地理范围示例:
guard let region = RectangularCoordinateRegion( northEast: CLLocationCoordinate2D(latitude: 40.9176, longitude: -73.7004), southWest: CLLocationCoordinate2D(latitude: 40.4774, longitude: -74.2591) ) else { print("Failed to create region.") return }
其他排查项
API密钥验证
- 确认Google Cloud控制台中已启用Places API(新版SDK依赖此API)
- 检查密钥的应用限制是否匹配你的iOS App Bundle ID
- 确认密钥无配额超限或余额不足问题
SDK版本检查
- 使用最新版的Google Places Swift SDK,旧版本可能存在内部逻辑bug
请求参数合法性
- 确保
includedType与isStrictTypeFiltering的组合符合SDK要求,避免传递冲突参数
- 确保
修正后的完整搜索函数示例
func searchForPizzaInNewYork() async { // 纽约市正确的地理边界 guard let region = RectangularCoordinateRegion( northEast: CLLocationCoordinate2D(latitude: 40.9176, longitude: -73.7004), southWest: CLLocationCoordinate2D(latitude: 40.4774, longitude: -74.2591) ) else { print("Failed to create region.") return } let searchByTextRequest = SearchByTextRequest( textQuery: "pizza", placeProperties: [.placeID], locationBias: region, includedType: .restaurant, maxResultCount: 2, isStrictTypeFiltering: true ) switch await PlacesClient.shared.searchByText(with: searchByTextRequest) { case .success(let places): for place in places { if let placeID = place.placeID { print("Place ID: \(placeID)") } } case .failure(let placesError): print("Error fetching places: \(placesError.localizedDescription)") print("Error details: \(placesError)") if let nsError = placesError as NSError? { print("Error code: \(nsError.code)") print("Error domain: \(nsError.domain)") print("User info: \(nsError.userInfo)") } } }
内容的提问来源于stack exchange,提问作者jonathan112358
相关产品推荐
相关产品推荐

