如何在Google Places API中使用VuetifyJS Advanced插槽示例

汤姆

如何在Google Places API中使用VuetifyJS Advanced插槽示例

CodePen示例

<v-autocomplete
  v-model="model"
  :items="items"
  :loading="isLoading"
  :search-input.sync="search"
  chips
  clearable
  hide-details
  hide-selected
  item-text="name"
  item-value="symbol"
  label="Search for a coin..."
  solo
>
  <template slot="no-data">
    <v-list-tile>
      <v-list-tile-title>
        Search for your favorite
        <strong>Cryptocurrency</strong>
      </v-list-tile-title>
    </v-list-tile>
  </template>
  <template
    slot="selection"
    slot-scope="{ item, selected }"
  >
    <v-chip
      :selected="selected"
      color="blue-grey"
      class="white--text"
    >
      <v-icon left>mdi-coin</v-icon>
      <span v-text="item.name"></span>
    </v-chip>
  </template>
  <template
    slot="item"
    slot-scope="{ item, tile }"
  >
    <v-list-tile-avatar
      color="indigo"
      class="headline font-weight-light white--text"
    >
      {{ item.name.charAt(0) }}
    </v-list-tile-avatar>
    <v-list-tile-content>
      <v-list-tile-title v-text="item.name"></v-list-tile-title>
      <v-list-tile-sub-title v-text="item.symbol"></v-list-tile-sub-title>
    </v-list-tile-content>
    <v-list-tile-action>
      <v-icon>mdi-coin</v-icon>
    </v-list-tile-action>
  </template>
</v-autocomplete>

我在Google Developer Console中添加了Google Maps地理编码API,Google Places API Web服务和Google Maps Javascript API,并收到了API密钥。

瓦迪姆·格雷米亚切夫(Vadim Gremyachev)

这是有关如何将Vuetify自动完成组件与Google Places API集成的说明

1)添加对Google Places API的引用

<script src="https://maps.googleapis.com/maps/api/js?&libraries=places&key=--KEY--"></script>

2)下面的示例显示如何v-autocomplete使用classgetPlacePredictions方法在位置填充组件AutocompleteService

search(val) {
  if (!val) {
      return;
  }

  this.isLoading = true;

  const service = new google.maps.places.AutocompleteService();
  service.getQueryPredictions({ input: val }, (predictions, status) => {
    if (status != google.maps.places.PlacesServiceStatus.OK) {
      return;
    }

    this.items = predictions.map(prediction => {
      return {
        id: prediction.id,
        name: prediction.description,
      };
    });

    this.isLoading = false;
  });
}

在此处输入图片说明

在CodePen上演示

先决条件

由于自动完成功能是Maps JavaScript API中Places库的功能,因此请首先确保在Google Cloud Platform Console中启用了Places API。请遵循此说明,以了解如何启用Places API

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章