如何搜索 Google Maps API 并获取多个地点?

Jon*_*man 5 google-maps-api-3 google-places-api google-places

我已经能够从 Google 地图向 Places API 发出请求,但我总是得到一个结果。

例如,搜索“白宫”或实际街道地址之类的内容,会得到一个结果:

https://maps.googleapis.com/maps/api/place/findplacefromtext/json
  ?input=the%20white%20house
  &inputtype=textquery
  &fields=formatted_address,name,geometry
  &key=API_KEY

response: {
  candidates: [
    {
      formatted_address: "1600 Pennsylvania Ave NW, Washington, DC 20500, USA",
      geometry: {
        location: {
          lat: 38.8976763,
          lng: -77.0365298
        },
        viewport: {
          northeast: {
            lat: 38.90148105,
            lng: -77.03520012010728
          },
          southwest: {
            lat: 38.89640805000001,
            lng: -77.03789977989273
          }
        }
      },
      name: "The White House"
    }
  ],
  status: "OK"
};
Run Code Online (Sandbox Code Playgroud)

如果我想找到我周围所有的星巴克怎么办?(如何限制搜索半径是后面的问题)

https://maps.googleapis.com/maps/api/place/findplacefromtext/json
  ?input=starbucks
  &inputtype=textquery
  &fields=formatted_address,name,geometry
  &key=API_KEY

response: {
  candidates: [],
  status: "ZERO_RESULTS"
};

Run Code Online (Sandbox Code Playgroud)

一些评论表明我的“星巴克”搜索应该得到不同的响应。为什么我不是?

小智 0

您可以尝试附近搜索。请注意,无法将附近搜索请求限制为仅返回特定字段。

只需查看下面的示例请求即可:

https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=38.8976763%2C-77.0365298&radius=50000&keyword=mcdonalds&key=YOUR_KEY

响应将为您提供此特定位置的所有麦当劳商店 38.8976763,-77.0365298。您还会注意到,有一个半径参数 50000 用于返回地点结果。

  • 据我所知,半径的最大值是50000,这并不是很多。因此,您无法获得一个国家/地区的所有多个地点。还有其他方法可以做到这一点吗? (2认同)