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

Google Places API仅返回2条结果问题排查求助

Google Places API Nearby Search仅返回2条结果的问题排查与解决

你遇到的问题核心在于Google Places Nearby Search的结果机制和参数配置,以下是具体原因和解决办法:

核心原因

  1. 未处理分页机制:Nearby Search默认最多返回20条结果,若结果总数超过20条,会返回next_page_token,需通过该token发起后续请求获取剩余结果。如果所有坐标都只返回2条,大概率是没处理分页逻辑;若仅特定区域如此,可能是该区域内locality类型地点本身数量较少。
  2. 参数限制冲突:使用radius参数时,API会优先返回范围内的地点,但部分区域符合条件的地点有限;另外radius与rankby=distance不能同时使用,后者会按距离排序返回更多结果。

解决办法

1. 处理分页请求

检查响应中的next_page_token字段,若存在则等待1-2秒(token需要短暂生效时间),用该token发起新请求合并结果:

// 初始请求
$nearbySearchUrl = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?" . http_build_query([
    'location' => $latitude . ',' . $longitude,
    'radius' => 50000,
    'type' => 'locality',
    'key' => $apiKey,
]);
$response = file_get_contents($nearbySearchUrl);
$data = json_decode($response, true);
$allResults = $data['results'];

// 检查并请求下一页
if (isset($data['next_page_token'])) {
    sleep(2);
    $nextPageUrl = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?" . http_build_query([
        'pagetoken' => $data['next_page_token'],
        'key' => $apiKey,
    ]);
    $nextResponse = file_get_contents($nextPageUrl);
    $nextData = json_decode($nextResponse, true);
    $allResults = array_merge($allResults, $nextData['results']);
}

// $allResults 包含所有获取到的结果

2. 调整搜索参数

  • 改用距离排序:去掉radius参数,添加rankby=distance,API会按距离从近到远返回最多60条结果(分3页):
$nearbySearchUrl = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?" . http_build_query([
    'location' => $latitude . ',' . $longitude,
    'rankby' => 'distance',
    'type' => 'locality',
    'key' => $apiKey,
]);
  • 替换地点类型:如果locality不符合需求,可尝试administrative_area_level_2(地区级行政区域)或administrative_area_level_3(小型城镇区域)。

3. 确认API配置

确保你的API密钥已启用Places API,且配额充足(配额不足会返回明确错误,而非少结果,但需排除此可能)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 06:05:07