【问题标题】:How to use VuetifyJS Advanced slots example with Google Places API如何通过 Google Places API 使用 VuetifyJS 高级插槽示例
【发布时间】:2019-05-21 07:36:24
【问题描述】:

如何将 VuetifyJS Advanced slots 示例与 Google Places API 一起使用?

CodePen example

<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 Geocoding API、Google Places API Web Service 和 Google Maps Javascript API,并收到了一个 API 密钥。

【问题讨论】:

  • 您想从 Google Places API 中显示什么?仅表示地名或其他任何内容?
  • 仅限地点(仅限文本)
  • 使用 AXIOS 获取数据并直接添加 API 密钥会暴露您的 API 密钥,浏览器也会根据 CORS 策略阻止它。您需要添加服务器端代码,该代码将充当代理服务器以从 Google Places API 获取数据。
  • 另外,我没有 Google Places API 的任何 API 密钥,否则我会相应地更新提供的 CodePen 中的代码。

标签: vue.js vuejs2 google-places-api vuetify.js


【解决方案1】:

以下是如何将Vuetify Autocompletes 组件与 Google Places API 集成的说明:

1) 添加对 Google Places API 的引用

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

2) 下面的例子展示了如何使用 AutocompleteService classgetPlacePredictions 方法将位置填充到 v-autocomplete 组件中

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;
  });
}

Demo on CodePen

先决条件

因为 自动完成是地图中 Places 库的一项功能 JavaScript API,首先确保在 谷歌云平台控制台。关注this instruction 了解如何 启用 Places API

【讨论】:

  • 我启用了 Places API 并按照您的步骤操作。输入地点后出现此错误:[Vue warn]: Error in callback for watcher "search": "TypeError: Cannot read property 'maps' of undefined" 有关如何修复此错误的任何想法?
  • 汤姆,你看过我回答的演示了吗?无论如何,如果您可以发布一个带有重现该问题的示例的新问题,我可以看看
  • 我看过你的演示。我复制了所有内容,所以我认为问题在于places.js 获取加法器或加载的方式。目前我把它放在 index.html 的 head 部分下面。 &lt;head&gt; &lt;script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=MYKEY&amp;libraries=places"&gt;&lt;/script&gt; 我在我的项目中使用 webpack。也许问题与此有关?不确定如何上传可复制的构建。
  • @Tom,也许你可以考虑在codesandbox.io上创建一个演示?
猜你喜欢
  • 2020-01-05
  • 2014-10-01
  • 1970-01-01
  • 2017-08-28
  • 1970-01-01
  • 2013-05-07
  • 2020-05-04
  • 2011-10-11
  • 1970-01-01
相关资源
最近更新 更多