You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用新版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
}

其他排查项

  1. API密钥验证

    • 确认Google Cloud控制台中已启用Places API(新版SDK依赖此API)
    • 检查密钥的应用限制是否匹配你的iOS App Bundle ID
    • 确认密钥无配额超限或余额不足问题
  2. SDK版本检查

    • 使用最新版的Google Places Swift SDK,旧版本可能存在内部逻辑bug
  3. 请求参数合法性

    • 确保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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 22:13:21