【问题标题】:How to find only school results in google autocomplete api?如何在谷歌自动完成 api 中仅查找学校结果?
【发布时间】:2019-02-12 09:05:15
【问题描述】:

我只想在 google 自动完成 api 中搜索学校。我已经尝试过,但它不会仅过滤特定于学校的搜索。我已从以下网址阅读

https://developers.google.com/places/web-service/autocomplete

但它只有types参数用于过滤结果。

我只能在 google 附近的地方搜索中搜索学校,但它需要我发送 lat,long 但我想搜索所有学校,无论位置如何。

【问题讨论】:

  • 您要搜索学校名称吗??
  • 是的。仅限学校

标签: google-maps google-maps-api-3 autocomplete google-places-api


【解决方案1】:

目前 Places API 自动完成功能不支持按学校、餐厅等类型进行过滤。

Google 问题跟踪器中有一个功能请求,可以按 Places API 搜索中支持的类型过滤自动完成功能:

https://issuetracker.google.com/issues/35820774

如您所见,该功能请求是在 2011 年提交的。不幸的是,Google 似乎并未对该任务设置高优先级。我建议为功能请求加注星标以添加您的投票。希望谷歌有一天会实现它。

【讨论】:

  • 是否可以获取通过 Places 自动完成返回的地点的地点 ID,然后使用地点 ID 查找地点类型并通过简单地附加到数组或其他内容进行过滤?
  • 我一直在寻找更多,谷歌似乎在使用自动完成时在返回的 JSON 数据中给出了地点类型。我假设你可以编写一个过滤器developers.google.com/places/web-service/autocomplete
  • @Junaid Google 在自动完成响应中没有详细的地点类型。它们只是为您提供通用类型,例如地理编码、机构等。您无法知道这是否是一所使用此字段的学校。
  • 是的,我现在才看到。如果您仍想过滤结果,您是否可以仅获取地点 ID,如果每个地点然后使用该地点类型获取地点类型。看起来应该可以了?
【解决方案2】:

这就是我发现对我有用的东西。不理想,但总比没有好

https://maps.googleapis.com/maps/api/place/textsearch/json?key=MY-APP-KEY&query=shaheen%20public&types=school

【讨论】:

  • 我认为这可能是您使用 Google API 进行学校搜索所能做到的最好的方法。出于某种原因,来自学校的各种 API 的结果似乎质量很差。 nearbysearchfindplacefromtext 的结果同样糟糕,通常只返回一个不正确的结果。
【解决方案3】:

你可以使用

https://maps.googleapis.com/maps/api/place/textsearch/json?key=MY-APP-KEY&query=shaheen%20public&types=school

在此处详细了解支持的类型。

https://developers.google.com/places/supported_types?csw=1

【讨论】:

    【解决方案4】:

    Google Places Autocomplete API 仍然不支持按类型过滤。我们使用的是一种解决方法。

    归结为获取给定输入的自动完成预测,然后过滤结果以仅获取学校。

    // types used in filtering autocomplete results
    const schoolPlaceTypes = [
      'school',
      'secondary_school',
      'university',
    ]
    
    // input event handler
    // DEBOUNCE THIS
    const searchSchool = async (key, query) => {
      const requestUrl = encodeURI(`https://maps.googleapis.com/maps/api/place/autocomplete/json?key=${key}&language=fr&input=${query}`)
      
      try {
        // fetch all predictions for a given input
        const response = await fetch(requestUrl, { method: 'GET' })
        const { predictions } = await response.json()
        // get all predictions that match at least on of the targeted types
        const results = predictions
          .filter(({ types }) => types.some(type => schoolPlaceTypes.includes(type)))
          // OPTIONAL: format filtered places to return only what is needed.
          .map(({ structured_formatting, place_id, types }) => ({
          place_id,
          name: structured_formatting.main_text,
          address: structured_formatting.secondary_text,
          types,
        }))
    
        return results
      }
      catch(e) {
        console.error(e)
        return []
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-13
      • 2012-01-07
      • 1970-01-01
      • 2014-06-28
      • 2011-09-19
      • 2016-11-17
      • 1970-01-01
      相关资源
      最近更新 更多