VUE H5 实时获取地理位置 不需要weixin-js-sdk 这个

navigator.geolocation是浏览器自带的 API,用于获取设备的地理位置信息,所以你提供的startLocationUpdate函数中使用的方法是浏览器自带的。

 startLocationUpdate() {
      if ("geolocation" in navigator) {
        let that = this; // 保持 this 的引用,如果您在类中使用,可以使用 this 直接调用方法
        navigator.geolocation.getCurrentPosition(
          (position) => {
            that.longitudeAbc = position.coords.longitude;
            that.latitudeAbc = position.coords.latitude;
            console.log("当前经度:" + that.longitudeAbc);
            console.log("当前纬度:" + that.latitudeAbc);

          },
          (error) => {
            alert("无法获取位置信息",error.message)
          }
        );
      }
    },

 它通过调用navigator.geolocation.getCurrentPosition方法来获取当前设备的位置信息。如果获取成功,会在回调函数中通过position.coords.longitudeposition.coords.latitude获取到经度和纬度,并将其分别赋值给that.longitudeAbcthat.latitudeAbc,同时在控制台打印出当前的经度和纬度。如果获取位置信息失败,则会在第二个回调函数中通过error.message弹出错误提示框,告知用户无法获取位置信息及具体的错误信息。

你可能感兴趣的:(javascript,vue.js,前端)