【发布时间】:2013-02-10 20:51:52
【问题描述】:
autocomplete = new google.maps.places.Autocomplete(input, { types: ['geocode'] });
返回街道和城市以及其他较大的区域。是否可以仅限于街道?
【问题讨论】:
-
嗯,你能解释一下吗?谷歌地方按定义的半径和类别为您提供地点列表。请举一个你尝试过的例子
autocomplete = new google.maps.places.Autocomplete(input, { types: ['geocode'] });
返回街道和城市以及其他较大的区域。是否可以仅限于街道?
【问题讨论】:
这个问题很老,但我想我会添加它以防其他人遇到这个问题。不幸的是,将类型限制为“地址”并没有达到预期的结果,因为仍然包括路线。因此,我决定做的是遍历结果并执行以下检查:
result.predictions[i].types.includes('street_address')
不幸的是,我很惊讶我自己的地址没有被包括在内,因为它返回以下类型:{ types: ['geocode', '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])
}
}
【讨论】:
我想你想要的是{ types: ['address'] }。
您可以通过此实时示例看到这一点:https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete(使用“地址”单选按钮)。
【讨论】:
{ types: ['address'] } 不会将结果限制为只有street_address 类型的结果。它还包括route 类型的结果,它们是没有数字的道路。
route?