【问题标题】:How to get current latitude and longitude in vuejs [closed]如何在vuejs中获取当前的纬度和经度[关闭]
【发布时间】:2020-10-10 09:16:49
【问题描述】:

我想要当前位置的纬度和经度。是否有任何插件或自定义代码可以做到这一点?

【问题讨论】:

    标签: vue.js vuejs2 vuex


    【解决方案1】:

    您可以使用WebGeolocation API如下:

    const vueComp = new Vue({
    
        data () {
            return {
                /* Reactive properties */
            };
        },
    
        created() {
    
            const success = (position) => {
                const latitude  = position.coords.latitude;
                const longitude = position.coords.longitude;
    
                // Do something with the position
            };
    
            const error = (err) => {
                console.log(error)
            };
    
            // This will open permission popup
            navigator.geolocation.getCurrentPosition(success, error);
        }
    
    });
    

    另外,如果你需要一个 promisified 插件,你可以使用vue-browser-geolocation plugin。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-11
      • 2011-08-31
      • 2017-05-09
      • 2012-07-08
      • 2015-09-07
      • 1970-01-01
      相关资源
      最近更新 更多