首页 文章

使用Google商家信息自动填充API的城市名称或所选城市的ID

提问于
浏览
1

我想使用Google商家信息自动填充API通过文本搜索来搜索位置 . 是否可以使用places autocomplete API在自动填充框中获取所选城市的城市名称或cityId?

1 回答

  • 1

    您可以通过 autocompleteInstance.getPlace() 访问当前地点的 address_components

    types - 属性(它是一个数组)包含值 politicallocality 的组件通常是城市 .

    例:

    google.maps.event.addListener(autocomplete, 'place_changed', function() {
      var components=this.getPlace().address_components,city='n/a';
          if(components){
            for(var c=0;c<components.length;++c){
            console.log(components[c].types.join('|'))
              if(components[c].types.indexOf('locality')>-1
                  &&
                 components[c].types.indexOf('political')>-1
                ){
                city=components[c].long_name;
                break;
              }
            }
          }
        alert(city)
      });
    

相关问题