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

Google Maps iOS SDK placesClient.fetchPlace无响应问题求助

Google Maps iOS SDK fetchPlace无响应排查方案

以下是核心排查方向,按优先级排序:

  • 确认placesClient实例是否有效
    代码里用了可选链placesClient?,如果这个实例是nil,请求根本不会发起,自然不会进入回调。检查初始化代码是否正确:

    // 标准初始化方式
    let placesClient = GMSPlacesClient.shared()
    

    同时确保在AppDelegate中完成了SDK基础配置,API密钥无拼写错误。

  • 检查Google Cloud的API配置

    • 确认API密钥已启用「Places API」服务,不要漏开
    • 密钥限制规则需匹配:Bundle ID必须和iOS项目完全一致,不要设置IP限制(iOS设备请求为动态IP,会直接被拦截)
    • 检查账户是否有可用额度,欠费或额度耗尽会导致请求被静默拒绝,不会返回错误信息
  • 验证PlaceID的有效性
    即便从Autocomplete获取ID,也不排除特殊地点被移除的情况。用该PlaceID在Google Places官方测试工具中查询,确认是否能返回正常结果。如果工具里查不到,问题出在Autocomplete环节。

  • 排查回调逻辑与线程问题

    • 如果代码里的print("bueller?")没输出,说明回调完全没触发,回到初始化/权限问题排查
    • 如果有输出但未进入error或place分支,可将字段组合简化为更安全的写法:let fields: GMSPlaceField = [.name, .coordinate, .placeID],避免手动转UInt的潜在错误
    • 确认completion闭包未被提前释放,比如是否存在循环引用或调用时闭包被销毁的情况

你的原始代码:

let placeID = placeIdString
// Specify the place data types to return.
let fields: GMSPlaceField = GMSPlaceField(rawValue: UInt(GMSPlaceField.name.rawValue) | UInt(GMSPlaceField.coordinate.rawValue) |
  UInt(GMSPlaceField.placeID.rawValue))

placesClient?.fetchPlace(fromPlaceID: placeID, placeFields: fields, sessionToken: nil, callback: {
  (place: GMSPlace?, error: Error?) in
    print("bueller?")
    if let error = error {
      print("An error occurred: \(error.localizedDescription)")
      completion(nil)
    }
    if let place = place {
      //self.lblName?.text = place.name
      print("The selected coord is: \(place.name ?? "nada") \(place.coordinate.latitude) \(place.coordinate.longitude)")
      //coordFound = place.coordinate
      completion(place.coordinate)
    }
})

内容的提问来源于stack exchange,提问作者MischkaTheBear

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.23 09:01:09