限制Google商家信息自动填充功能仅返回地址

时间:2013-02-25 16:26:48

标签: google-places-api

autocomplete = new google.maps.places.Autocomplete(input, { types: ['geocode'] });

返回街道和城市以及其他更大的区域。是否可以仅限制街道?

2 个答案:

答案 0 :(得分:4)

我认为你想要的是{ types: ['address'] }

您可以使用此实时示例查看此操作:https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete(使用“地址”单选按钮)。

答案 1 :(得分:0)

这个问题很旧,但是我想我可以补充一下,以防其他人遇到这个问题。不幸的是,将类型限制为“ address”并不能达到预期的结果,因为仍然包括路由。因此,我决定要做的就是遍历结果并执行以下检查:

result.predictions[i].types.includes('street_address')

不幸的是,我很惊讶得知自己的地址未包含在内,因为它返回了以下类型:{ types: ['geocode', 'premise'] }

因此,我决定启动一个计数器,并且在其类型中包含“地理编码”或“路由”的任何结果都必须至少包含一个其他术语(无论是“ street_address”还是“ premise”或其他任何术语)因此,路由被排除在外,任何包含完整地址的内容都将被包括在内。虽然不是万无一失,但效果很好。

遍历结果预测,并执行以下操作:

if (result.predictions[i].types.includes('street_address')) {
    // Results that include 'street_address' should be included
    suggestions.push(result.predictions[i])
} else {
    // Results that don't include 'street_address' will go through the check
    var typeCounter = 0;
    if (result.predictions[i].types.includes('geocode')) {
        typeCounter++;
    }
    if (result.predictions[i].types.includes('route')) {
        typeCounter++;
    }
    if (result.predictions[i].types.length > typeCounter) {
        suggestions.push(result.predictions[i])
    }
}