zoukankan      html  css  js  c++  java
  • 小程序获取用户的地理位置与商家的相距距离

    getUserLocation: function () {
    let vm = this;
    wx.getSetting({
    success: (res) => {
    console.log(JSON.stringify(res))
    // res.authSetting['scope.userLocation'] == undefined 表示 初始化进入该页面
    // res.authSetting['scope.userLocation'] == false 表示 非初始化进入该页面,且未授权
    // res.authSetting['scope.userLocation'] == true 表示 地理位置授权
    if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
    wx.showModal({
    title: '请求授权当前位置',
    content: '需要获取您的地理位置,请确认授权',
    success: function (res) {
    if (res.cancel) {
    wx.showToast({
    title: '拒绝授权',
    icon: 'none',
    duration: 1000
    })
    } else if (res.confirm) {
    wx.openSetting({
    success: function (dataAu) {
    if (dataAu.authSetting["scope.userLocation"] == true) {
    wx.showToast({
    title: '授权成功',
    icon: 'success',
    duration: 1000
    })
    //再次授权,调用wx.getLocation的API
    vm.getLocation();
    } else {
    wx.showToast({
    title: '授权失败',
    icon: 'none',
    duration: 1000
    })
    }
    }
    })
    }
    }
    })
    } else if (res.authSetting['scope.userLocation'] == undefined) {
    //调用wx.getLocation的API
    vm.getLocation();
    }
    else {
    //调用wx.getLocation的API
    vm.getLocation();
    }
    }
    })
    },
     
    // 获取用户的当前位置
    getLocation: function () {
    let that= this;
    wx.getLocation({

    type: 'wgs84',
    success: function (res) {
    var la1 = res.latitude
    var lo2 = res.longitude
    that.setData({
    la1: la1,
    lo2: lo2,
    scale:28
    })
    that.getShangjia(la1, lo2);
    }

    })
    },
    distance: function (la1, lo1, la2, lo2) {
    var La1 = la1 * Math.PI / 180.0;
    var La2 = la2 * Math.PI / 180.0;
    var La3 = La1 - La2;
    var Lb3 = lo1 * Math.PI / 180.0 - lo2 * Math.PI / 180.0;
    var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(La3 / 2), 2) + Math.cos(La1) * Math.cos(La2) * Math.pow(Math.sin(Lb3 / 2), 2)));
    s = s * 6378.137;//地球半径
    s = Math.round(s * 10000) / 10000;
    console.log("计算结果", s)
    this.setData({
    s: s
    })
    },
     
    //商家
    getShangjia: function (la1, lo2) {
    var that = this;
    app.util.request({
    'url': 'entry/wxapp/Shangjia',
    success: function (res) {
    console.log(res.data.data);
    var shangjia = res.data.data
    for (var i = 0; i < shangjia.length; i++) {
    // la2 = shangjia[i].s_latitude,
    // lo2 = shangjia[i].s_longitude
    that.distance(la1, lo2, shangjia[i].s_latitude, shangjia[i].s_longitude)
    shangjia[i].s_shopjuli = that.data.s;
    }
    that.setData({
    shangjia: res.data.data
    })
     
    },
    fail: function (err) {
    console.log(err)
    },
    });
    },

    // // 获取用户的当前位置
    // wx.getLocation({
     
    // type: 'wgs84',
    // success: function (res) {
    // var la1 = res.latitude
    // var lo2 = res.longitude
    // that.setData({
    // la1: la1,
    // lo2: lo2,
    // scale:28
    // })
    // that.getShangjia(la1, lo2);
    // }
     
    // })
  • 相关阅读:
    转:Ubuntu12.04编译VLC,在linux上运行
    samba 安装运行
    设计模式学习笔记 1.factory 模式
    python之字符串的拼接总结
    str函数之不同变量之间如何连接,外加浮点运算注意事项
    python的安装以及前景
    input函数的运用和注意 小知识点
    mysql基础篇(上篇)
    接口测试基本安全
    jmeter接口自动化测试
  • 原文地址:https://www.cnblogs.com/isuansuan/p/9940175.html
Copyright © 2011-2022 走看看