Google Places Find Place API传入非废弃字段时触发fields参数解析错误
Google Places findplacefromtext接口传参报错排查
问题表现
调用Google Places findplacefromtext(文本搜索地点)API时,仅传入基础参数可正常运行;在fields参数中新增international_phone_number、website两个字段后接口调用失败,返回错误如下:
{ "error_message": "Error while parsing 'fields' parameter: Unsupported field name 'international_phone_number'. ", "status": "INVALID_REQUEST" }
对应调用代码如下:
let input = name; let fields = "name%2cformatted_address%2Cplace_id%2Cbusiness_status%2Cinternational_phone_number%2Cwebsite"; let locationBias = "circle%3A1000%40" + latitude + "%2C" + longitude; let url = "https://maps.googleapis.com/maps/api/place/findplacefromtext/json?fields=" + fields + "&language=en&input=" + input + "&inputtype=textquery&locationbias=" + locationBias + "&key=" + placesApiKey; function getData(url) { fetch(url, { "headers": { }, "body": null, "method": "GET" }) .then(response => response.json()) .then(data => console.log(data)) .catch(err => console.log(err)); } getData(url);
已知前提
- 移除
fields参数中的international_phone_number、website字段后,接口可正常执行成功 - 已核实查询目标对应的Google Place对象确实包含上述两个字段,二者不属于已废弃的无效字段
根本原因
findplacefromtext接口本身的fields参数支持列表里,不包含international_phone_number、website这两个字段。
这两个字段属于地点详情类接口的返回范畴:findplacefromtext是模糊搜索匹配接口,只返回基础的地点匹配信息,不对外开放联系方式、官网链接这类详情字段的返回能力,和目标地点本身是否有这两个属性无关,只要在这个接口的fields里传了不支持的字段名,就会直接报参数非法错误。
解决方案
- 调整
findplacefromtext接口的fields参数,仅保留该接口支持的字段(比如你之前使用的name、formatted_address、place_id、business_status),通过该接口拿到匹配地点的唯一标识place_id - 拿到
place_id后,调用Google Places的地点详情接口,在详情接口的fields参数中传入international_phone_number、website以及你需要的其他字段,即可获取到对应的手机号、官网信息 - 代码优化建议:拼接URL参数时不需要手动编写转义字符,直接用JS原生的
encodeURIComponent()方法处理参数值即可,避免手动转义时混用大小写、漏转特殊字符导致的额外参数解析问题。
内容的提问来源于stack exchange,提问作者Jake L
相关产品推荐
相关产品推荐

