繁体   English   中英

在 Hybrid 移动应用中使用 VueJs 实现谷歌地点

[英]Implementation of google places using VueJs in Hybrid mobile application

我正在尝试在混合移动应用程序中使用 vue js 实现对谷歌地点的搜索。 搜索功能工作正常,但是当我单击地点列表时,我无法选择其中任何一个。

我使用过vue-google-places库和vue-google-autocomplete库。 在两个图书馆中,我都面临着同样的问题。

<template>
  <q-item>
    <q-item-main>
      <q-field :error="$v.serial.$error">
        <VueGooglePlaces
          api-key="api-key"
          class="subheading"
          :enable-geolocation="true"
          types="(cities)"
          @placechanged="onPlaceChanged"
        >
          <input
            ref="address"
            float-label="Serial Number"
            type="text"
            v-model="address"
            clearable
            @blur="$v.serial.$touch"
          />
        </VueGooglePlaces>

        <vue-google-autocomplete
          id="map"
          class="form-control"
          placeholder="Start typing"
          v-on:placechanged="getAddressData"
        ></vue-google-autocomplete>
      </q-field>
    </q-item-main>
  </q-item>
</template>

import VueGoogleAutocomplete from "vue-google-autocomplete";
import Vue from "vue";

export default {
  name: "ScanBarcodePage",
  components: {
    "page-title": PageTitleComponent
  },
  mounted() {
    if (this.$refs.address) {
      this.$refs.address.focus();
    }
  },
  data() {
    return {
      address: "",
      serial: "",
      places: []
    };
  },
  methods: {
    getAddressData: function(addressData, placeResultData, id) {
      this.address = addressData.formatted_address;
    },
    onPlaceChanged: function(addressData, placeResultData, id) {
      this.address = addressData.formatted_address;
    }
  }
}

我已经添加了上面的示例代码。 我没有收到任何错误,但是当我在文本字段中输入一些字符时,它会显示地点列表,但我无法选择其中任何一个。 但是相同的插件在浏览器上运行良好。

在此处输入图片说明

我的猜测是,由于两次调用 Maps API,这些库会相互冲突。 尝试仅实施其中之一以排除这种情况。

这是我通过 Apache cordova 在混合应用程序中使vue-google-autocomplete在移动设备(Android)上运行的工作。

首先是一些上下文,我通过vue-cli-plugin-cordova创建了一个 vue +cordova 应用程序。

/public/index.html ,在<head>元素中添加带有有效API 密钥的 Maps API 脚本:

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

然后转到/src/App.vue并用库文档示例中的代码替换所有内容:

<template>
    <div>
        <h2>Your Address</h2>

        <vue-google-autocomplete
            ref="address"
            id="map"
            classname="form-control"
            placeholder="Please type your address"
            v-on:placechanged="getAddressData"
            country="sg"
        >
        </vue-google-autocomplete>
    </div>
</template>

<script>
    import VueGoogleAutocomplete from 'vue-google-autocomplete'

    export default {
        components: { VueGoogleAutocomplete },

        data: function () {
            return {
              address: ''
            }
        },

        mounted() {
            // To demonstrate functionality of exposed component functions
            // Here we make focus on the user input
            this.$refs.address.focus();
        },

        methods: {
            /**
            * When the location found
            * @param {Object} addressData Data of the found location
            * @param {Object} placeResultData PlaceResult object
            * @param {String} id Input container ID
            */
            getAddressData: function (addressData, placeResultData, id) {
                this.address = addressData;
            }
        }
    }
</script>

然后插入设备(或模拟器)并运行您的应用程序。 您应该能够毫无问题地从自动完成预测中选择一个地点。

如果您仍然不能,请检查您的 API 密钥是否确实有效,即它必须受到适当(未)限制并且属于启用了计费、JavaScript API 和 Places API 的 Cloud 项目。 更多信息请参阅此处的 Google 指南

真的希望这对你有帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM