使用google maps api v3中的自动完成服务获取邮政编码

loi*_*icb 6 javascript google-maps-api-3

我正在使用google maps api V3中的autocompleteservice来进行自定义自动完成输入.我基本上调用此函数来检索城市建议.

function getPlaces(st){
    gService.getQueryPredictions({input:st,types:['geocode']},function(predictions,status)      {
        if(status != google.maps.places.PlacesServiceStatus.OK) return false;
        for(var i = 0, prediction; prediction = predictions[i]; i++){
            console.log(prediction);
        }
        return predictions;
    });
}
Run Code Online (Sandbox Code Playgroud)

这很好用,但我希望得到与城市相关的邮政编码.与V2 api一样,请参阅此示例.

我试图将类型选项更改为城市,但结果是相同的.知道怎么做吗?

Ale*_*dra 1

在新的 API 中,AutocompleteService 仅搜索与您目前输入的文本相匹配的猜测字符串。要获取包含地址和其他数据的实际 Place 对象,您需要使用将 autocomplete() 函数附加到文本框时创建的 Autocomplete 对象。请参阅此处的文档:https ://developers.google.com/maps/documentation/javascript/places#getting_place_information