国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

微信小程序如何使用地球半徑計算兩組經(jīng)緯度點(diǎn)之間的距離(自身位置與接口返回位置)【上】

這篇具有很好參考價值的文章主要介紹了微信小程序如何使用地球半徑計算兩組經(jīng)緯度點(diǎn)之間的距離(自身位置與接口返回位置)【上】。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報違法"按鈕提交疑問。

微信小程序如何使用地球半徑計算兩組經(jīng)緯度點(diǎn)之間的距離(自身位置與接口返回位置)【上】,微信小程序,apache,小程序,前端,微信公眾平臺,微信開放平臺

目錄

1.配置位置權(quán)限?

2.獲取當(dāng)前自身經(jīng)緯度?

3. 請求接口拿到返回經(jīng)緯

4. 循環(huán)取每一項(xiàng)的經(jīng)緯

5.如何判斷是否打開了定位權(quán)限?

6.進(jìn)行距離計算操作?

7.運(yùn)行效果

8.完整代碼

首先在使用小程序時,請求的接口一定要去配置合法域名,才能夠進(jìn)行接下來的操作。?

1.配置位置權(quán)限?

在app.json中添加如下代碼:

 "permission": {
    "scope.userLocation": {
      "desc": "你的位置信息將用于小程序位置接口的效果展示"
    },
  }

2.獲取當(dāng)前自身經(jīng)緯度?

我們通過wx.getLocation這個api進(jìn)行獲?。?

 wx.getLocation({
      type: 'gcj02',
      success(res) {
        that.setData({
          currentLatitude: res.latitude,
          currentLongitude: res.longitude
        });
})

在這里我們使用了變量來存放當(dāng)前經(jīng)緯度,并且使用that.setData({})方法來賦值,因?yàn)閠his的指向可能不明確,配置如下:

 data: {
      currentLatitude:'',
      currentLongitude:'',
}

  onLoad(options) {
    let that = this;
}

除了gcj02,還有以下幾種地理位置坐標(biāo)系類型可以選擇:

  • wgs84:表示使用WGS84坐標(biāo)系,也是全球衛(wèi)星定位系統(tǒng)(GPS)所采用的坐標(biāo)系。
  • bd09:表示使用百度坐標(biāo)系,在國內(nèi)常用的一種坐標(biāo)系,適用于百度地圖。
  • bd09ll:表示使用百度經(jīng)緯度坐標(biāo)系,與bd09相似,但在經(jīng)度上更接近真實(shí)的經(jīng)度值。

3. 請求接口拿到返回經(jīng)緯

wx.request({
          url: '自己所使用的接口',
          method: 'POST',
          data: {},
          success: function(res) {
            console.log(res);
            that.setData({
              list: res.data.data.list
            });
}
})

上述代碼呢list是我在data中的一個變量,用來存放我包含經(jīng)緯度數(shù)據(jù)的數(shù)組,聲明方式為list:[]

4. 循環(huán)取每一項(xiàng)的經(jīng)緯

?list數(shù)據(jù)格式如下:微信小程序如何使用地球半徑計算兩組經(jīng)緯度點(diǎn)之間的距離(自身位置與接口返回位置)【上】,微信小程序,apache,小程序,前端,微信公眾平臺,微信開放平臺?

 for (let i = 0; i < that.data.list.length; i++) {
              let item = that.data.list[i];
              let lat = parseFloat(item.latitude);
              let lon = parseFloat(item.longitude);
            }
            that.setData({
              list: that.data.list
            });

item.latitudeitem.longitude轉(zhuǎn)換為浮點(diǎn)數(shù)(parseFloat())的目的是將字符串轉(zhuǎn)換為數(shù)值型數(shù)據(jù)。這是因?yàn)榻?jīng)緯度通常以字符串的形式存儲,在進(jìn)行距離計算等數(shù)值操作時,需要將其轉(zhuǎn)換為數(shù)值類型進(jìn)行計算。通過使用parseFloat()函數(shù),可以將字符串解析成浮點(diǎn)數(shù),以便后續(xù)的數(shù)值計算。?

5.如何判斷是否打開了定位權(quán)限?

?在每一次打開此頁面的時候都進(jìn)行一次權(quán)限排查,如果沒有打開定位權(quán)限,則會調(diào)起設(shè)置打開權(quán)限

checkLocationPermission() {
    let that = this;
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.userLocation']) {
          wx.showModal({
            title: '提示',
            content: '您還沒有啟用定位權(quán)限,是否前往設(shè)置開啟?',
            success(res) {
              if (res.confirm) {
                // User confirmed opening settings
                wx.openSetting({
                  success(res) {
                    if (res.authSetting['scope.userLocation']) {
                      // User granted location permission in settings
                      that.onLoad(); // Reload the page to fetch data with location permission
                    } else {
                      // User did not grant location permission in settings
                      wx.showToast({
                        title: '您還未開啟定位權(quán)限',
                        icon: 'none'
                      });
                    }
                  }
                });
              } else if (res.cancel) {
                console.log(res.cancel);
                // User canceled opening settings
                wx.showToast({
                  title: '您還未開啟定位權(quán)限',
                  icon: 'none'
                });
              }
            }
          });
        }
      }
    });
  },

因?yàn)榇舜a是排查權(quán)限信息,所以單獨(dú)寫了個函數(shù),你可以選擇在onshow或者onload中調(diào)用

  onShow() {
    this.checkLocationPermission();
  },

6.進(jìn)行距離計算操作?

  getDistance: function(lat1, lon1, lat2, lon2) {
    var R = 6371;
    var dLat = (lat2 - lat1) * Math.PI / 180;
    var dLon = (lon2 - lon1) * Math.PI / 180;
    var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
            Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) *
            Math.sin(dLon / 2) * Math.sin(dLon / 2);
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    var distance = R * c; 
    return distance.toFixed(2);
  },

詳解代碼:

  • var R = 6371;:定義地球的半徑,單位為公里。這是用于計算兩點(diǎn)間距離的基準(zhǔn)。

  • var dLat = (lat2 - lat1) * Math.PI / 180;:計算兩點(diǎn)緯度差值,并將其轉(zhuǎn)換為弧度值。

  • var dLon = (lon2 - lon1) * Math.PI / 180;:計算兩點(diǎn)經(jīng)度差值,并將其轉(zhuǎn)換為弧度值。

  • var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) * Math.sin(dLon / 2) * Math.sin(dLon / 2);:根據(jù)Haversine公式,計算兩點(diǎn)間的距離。

    • Math.sin(dLat / 2) * Math.sin(dLat / 2):計算緯度差值的一半的正弦平方。
    • Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180):計算兩點(diǎn)緯度的余弦乘積。
    • Math.sin(dLon / 2) * Math.sin(dLon / 2):計算經(jīng)度差值的一半的正弦平方。
    • 將上述三個部分相加得到a的值,表示兩點(diǎn)間的角距離。
  • var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));:根據(jù)球面三角學(xué)公式,計算角距離對應(yīng)的弧長。

    • Math.sqrt(a):計算a的平方根。
    • Math.sqrt(1 - a):計算1 - a的平方根。
    • Math.atan2():計算給定坐標(biāo)的反正切值。
  • var distance = R * c;:將弧長乘以地球半徑,得到兩點(diǎn)間的直線距離,單位為公里。

  • return distance.toFixed(2);:返回計算得到的距離,并將其保留兩位小數(shù)。

?最后需要調(diào)用函數(shù)到接口操作中(上述的for循環(huán)):

for (let i = 0; i < that.data.list.length; i++) {
              let item = that.data.list[i];
              let lat = parseFloat(item.latitude);
              let lon = parseFloat(item.longitude);
              let distance = that.getDistance(that.data.currentLatitude, that.data.currentLongitude, lat, lon);
              item.distance = distance;
            }
            that.setData({
              list: that.data.list
            });

7.運(yùn)行效果

微信小程序如何使用地球半徑計算兩組經(jīng)緯度點(diǎn)之間的距離(自身位置與接口返回位置)【上】,微信小程序,apache,小程序,前端,微信公眾平臺,微信開放平臺

8.完整代碼

Page({

  /**
   * 頁面的初始數(shù)據(jù)
   */
  data: {
      list:[],
      currentLatitude:'',
      currentLongitude:'',
     
  },

  /**
   * 生命周期函數(shù)--監(jiān)聽頁面加載
   */
  handleImageError(e){
  onLoad(options) {
    let that = this;
    wx.getLocation({
      type: 'gcj02',
      success(res) {
        that.setData({
          currentLatitude: res.latitude,
          currentLongitude: res.longitude
        });
        
        wx.request({
          url: '',
          method: 'POST',
          data: {},
          success: function(res) {
            console.log(res);
            that.setData({
              list: res.data.data.list
            });
            
            for (let i = 0; i < that.data.list.length; i++) {
              let item = that.data.list[i];
              let lat = parseFloat(item.latitude);
              let lon = parseFloat(item.longitude);
              let distance = that.getDistance(that.data.currentLatitude, that.data.currentLongitude, lat, lon);
              item.distance = distance;
            }
            that.setData({
              list: that.data.list
            });
          },
        });
      },
      fail(res) {
        wx.showModal({
          title: '提示',
          content: '您還沒有啟用定位權(quán)限,是否前往設(shè)置開啟?',
          success(res) {
            if (res.confirm) {
              wx.openSetting({
                success(res) {
                  if (res.authSetting['scope.userLocation']) {
                    that.onLoad(options); 
                  } else {
                    wx.showToast({
                      title: '您還未開啟定位權(quán)限',
                      icon: 'none'
                    });
                  }
                }
              });
            } else if (res.cancel) {
              console.log(res.cancel);
              wx.showToast({
                title: '您還未開啟定位權(quán)限',
                icon: 'none'
              });
            }
          }
        });
      }
    });
  },
  
  onShow() {
    this.checkLocationPermission();
  },
  
  checkLocationPermission() {
    let that = this;
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.userLocation']) {
          wx.showModal({
            title: '提示',
            content: '您還沒有啟用定位權(quán)限,是否前往設(shè)置開啟?',
            success(res) {
              if (res.confirm) {
                wx.openSetting({
                  success(res) {
                    if (res.authSetting['scope.userLocation']) {
                      that.onLoad(); 
                    } else {
                      wx.showToast({
                        title: '您還未開啟定位權(quán)限',
                        icon: 'none'
                      });
                    }
                  }
                });
              } else if (res.cancel) {
                console.log(res.cancel);
                wx.showToast({
                  title: '您還未開啟定位權(quán)限',
                  icon: 'none'
                });
              }
            }
          });
        }
      }
    });
  },
  
  getDistance: function(lat1, lon1, lat2, lon2) {
    var R = 6371; 
    var dLat = (lat2 - lat1) * Math.PI / 180;
    var dLon = (lon2 - lon1) * Math.PI / 180;
    var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
            Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) *
            Math.sin(dLon / 2) * Math.sin(dLon / 2);
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    var distance = R * c;
    return distance.toFixed(2);
  },
  /**
   * 生命周期函數(shù)--監(jiān)聽頁面初次渲染完成
   */
  

  onReady() {

  },

  /**
   * 生命周期函數(shù)--監(jiān)聽頁面顯示
   */
  onShow() {

  },

  /**
   * 生命周期函數(shù)--監(jiān)聽頁面隱藏
   */
  onHide() {

  },

  /**
   * 生命周期函數(shù)--監(jiān)聽頁面卸載
   */
  onUnload() {

  },

  /**
   * 頁面相關(guān)事件處理函數(shù)--監(jiān)聽用戶下拉動作
   */
  onPullDownRefresh() {

  },

  /**
   * 頁面上拉觸底事件的處理函數(shù)
   */
  onReachBottom() {

  },

  /**
   * 用戶點(diǎn)擊右上角分享
   */
  onShareAppMessage() {

  }
})

微信小程序如何使用地球半徑計算兩組經(jīng)緯度點(diǎn)之間的距離(自身位置與接口返回位置)【上】,微信小程序,apache,小程序,前端,微信公眾平臺,微信開放平臺

微信小程序如何利用接口返回經(jīng)緯計算實(shí)際位置并且進(jìn)行導(dǎo)航功能【下】
https://blog.csdn.net/m0_71966801/article/details/134084525?spm=1001.2014.3001.5502?文章來源地址http://www.zghlxwxcb.cn/news/detail-739256.html

到了這里,關(guān)于微信小程序如何使用地球半徑計算兩組經(jīng)緯度點(diǎn)之間的距離(自身位置與接口返回位置)【上】的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 微信小程序如何使用SCSS

    微信小程序如何使用SCSS

    微信小程序開發(fā)者工具集成了 vscode 編輯器,可以使用 vscode 中眾多的插件,為我們開發(fā)微信小程序提供了極大的便利。我們可以借助 easysass 插件實(shí)現(xiàn)在微信開發(fā)中使用 sass,安裝步驟如下。 1,在 vscode 中安裝 easysass 插件。 2,導(dǎo)入已安裝的vscode擴(kuò)展 接下來打開微信開發(fā)者工

    2024年02月12日
    瀏覽(20)
  • 微信小程序里面如何使用svg圖片

    微信小程序里面如何使用svg圖片

    首先準(zhǔn)備一段svg代碼如下: 然后按照下面的格式更改 也打開下面的網(wǎng)址轉(zhuǎn), https://codepen.io/jakob-e/pen/doMoML 將svg代碼貼到下圖中紅線圈起來的位置,會自動轉(zhuǎn)成base64, 然后將base64代碼部分復(fù)制下來放到瀏覽器里面打開,可以看到結(jié)果. 最后在小程序里面使用,如下 結(jié)果:

    2024年02月11日
    瀏覽(31)
  • 微信小程序使用 checkbox 如何修改樣式?

    微信小程序使用 checkbox 如何修改樣式?

    主要通過 .wx-checkbox-input 、 .wx-checkbox-input-checked 以及 .wx-checkbox-input-checked::before 三個類名來設(shè)置 checkbox 的樣式。 .wx-checkbox-input 用于設(shè)置未選中時框的樣式 .wx-checkbox-input-checked 用于設(shè)置選中后框的樣式 ,為了覆蓋原生樣式,需要與 .wx-checkbox-input 類連寫,否則權(quán)重不夠。 .

    2024年02月12日
    瀏覽(91)
  • 微信小程序使用--如何生成二維碼

    微信小程序使用--如何生成二維碼

    一、生成二維碼 1.獲取token 參照官方文檔說明: https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/mp-access-token/getAccessToken.html 其中g(shù)rant_type是寫死的,appid和secret是注冊小程序的時候獲取的,然后會得到一個默認(rèn)兩小時失效的token 2.獲取二維碼 參照官方文檔說明: https://developers.

    2024年02月04日
    瀏覽(23)
  • 微信小程序如何配置并使用less?

    微信小程序如何配置并使用less?

    如果以上步驟完成后 未實(shí)現(xiàn)最后一步,建議重啟微信開發(fā)者工具 好了,這次分享到這里就結(jié)束了。如果你覺得對你有幫助,請給我點(diǎn)個贊并留言告訴我,讓我為你開心下??。

    2024年02月13日
    瀏覽(17)
  • 微信小程序開發(fā)系列(十三)·如何使用iconfont、微信小程序中如何使用字體圖標(biāo)

    微信小程序開發(fā)系列(十三)·如何使用iconfont、微信小程序中如何使用字體圖標(biāo)

    目錄 1.? 如何使用iconfont 2.??微信小程序中如何使用字體圖標(biāo) 3.? 背景圖的使用 ????????在項(xiàng)目中使用到的小圖標(biāo),一般由公司設(shè)計師進(jìn)行設(shè)計,設(shè)計好以后上傳到阿里巴巴矢量圖標(biāo)庫,然后方便程序員來進(jìn)行使用。 ????????小程序中的字體圖標(biāo)使用方式與 Web 開發(fā)中

    2024年03月20日
    瀏覽(109)
  • 微信小程序中使用 wx.getLocation獲取當(dāng)前詳細(xì)位置并計算距離

    微信小程序中使用 wx.getLocation獲取當(dāng)前詳細(xì)位置并計算距離

    wx.getLocation只能夠獲取經(jīng)緯度,不能夠拿到詳細(xì)地址;如果你的項(xiàng)目剛好也使用騰訊地圖的api,那么可以通過騰訊地圖的逆解析就能拿到詳細(xì)地址了; 先介紹一下wx.getLocation()方法的使用; 此方法可以獲取當(dāng)前的經(jīng)緯度和速度、高度;官網(wǎng)鏈接 想要使用這個方法,先需要在

    2024年02月08日
    瀏覽(96)
  • 【微信小程序】詳解behaviors,如何使用behaviors

    【微信小程序】詳解behaviors,如何使用behaviors

    behaviors 是小程序中, 用于實(shí)現(xiàn)組件間代碼共享的特性 ,類似于 Vue.js 中的 “mixins”。 每個 behavior 可以包含一組 屬性、數(shù)據(jù)、生命周期函數(shù)和方法 。組件引用它時,它的屬性、數(shù)據(jù)和方法 會被合并到組件中 。每個組件可以引用多個 behavior,behavior 也可以引用其它 behavior。

    2023年04月23日
    瀏覽(18)
  • 微信小程序如何使用scss編譯wxss文件

    微信小程序如何使用scss編譯wxss文件

    微信小程序開發(fā)者工具集成了 vscode 編輯器,可以使用 vscode 中眾多的插件,為我們開發(fā)微信小程序提供了極大的便利。我們可以借助 easysass 插件實(shí)現(xiàn)在微信開發(fā)中使用 sass,安裝步驟如下。 1.在 vscode 中搜索 easysass 插件并安裝 2.導(dǎo)入已安裝的vscode擴(kuò)展 微信開發(fā)者工具 視圖

    2024年02月08日
    瀏覽(26)
  • 微信小程序如何使用web-view

    微信小程序如何使用web-view

    ? ? ? ?有時我們的業(yè)務(wù)需求是小程序內(nèi)部跳轉(zhuǎn)到其他h5項(xiàng)目的頁面,這是我們就會用到web-view標(biāo)簽,這個標(biāo)簽可以幫助我們完成h5頁面的渲染。下面,就是使用方法: 第一步: 首先現(xiàn)在小程序建一個新的page用來使用web-view ?在wxml文件中寫入web-view標(biāo)簽,src屬性為你要跳轉(zhuǎn)h5線

    2024年02月14日
    瀏覽(24)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包