JS获取当前地理位置(经纬度)

if(navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(
        function (position) {  
            console.log( position.coords.longitude );
            console.log( position.coords.latitude );
        },
        function (e) {
           throw(e.message);
        }
    ) 
}

微信小程序获取当前地理位置(经纬度)

wx.getLocation({
      altitude: 'altitude',
      isHighAccuracy: true, 
      success(res) {
        // 手机经纬度定位更加精准,需要截取到小数点后六位
        var currentLatitude = (res.latitude).toFixed(6);  //纬度
        var currentLongitude = (res.longitude).toFixed(6); //经度
        console.log(currentLatitude , currentLongitude)
        this.setData({
          // 经度
          longitude: currentLongitude,
          // 纬度
          latitude: currentLatitude
        })
      }
    })

微信小程序通过经纬度逆推获取当前城市信息

  1. 首先需要去腾讯位置服务官网申请开发者密钥
  2. 开通webserviceAPI服务:在控制台 -> key管理 -> 设置(使用该功能的key)-> 勾选webserviceAPI -> 保存。(小程序SDK需要用到webserviceAPI的部分服务,所以使用该功能的KEY需要具备相应的权限)
  3. 下载微信小程序JavaScriptSDK,微信小程序JavaScriptSDK v1.1 JavaScriptSDK v1.2
  4. 安全域名设置,在小程序管理后台 -> 开发 -> 开发管理 -> 开发设置 -> “服务器域名” 中设置request合法域名,添加https://apis.map.qq.com
  5. 示例如下
getAddress() {
    var that = this
    <!-- 调用微信小程序获取经纬度API -->
    wx.getLocation({
      altitude: 'altitude',
      isHighAccuracy: true, 
      success(res) {
        // 手机经纬度定位更加精准,需要截取到小数点后六位
        var currentLatitude = (res.latitude).toFixed(6);  //纬度
        var currentLongitude = (res.longitude).toFixed(6); //经度
        <!-- 将经纬度存储在data中 -->
        that.setData({
          // 经度
          longitude: currentLongitude,
          // 纬度
          latitude: currentLatitude
        })
        // 逆解析接口 /ws/geocoder/v1
        <!-- 定义一个变量用于拼接地址逆解析接口,key值是腾讯位置服务中注册后生成的 key -->
        var qqMapApi = 'http://apis.map.qq.com/ws/geocoder/v1/' + "?location=" + that.data.latitude + ',' +
        that.data.longitude + "&key=" + 'ZHHBZ-MCN6D-HUX4Y-PL7SW-KM4TQ-WYBDZ' + "&get_poi=1";
        <!-- 拼接完成后作为参数使用 -->
        that.getSkys(qqMapApi);
      }
    })
  },
  <!-- 调用接口将参数作为 url 发送请求 -->
  getSkys(qqMapApi){
    const that = this
    wx.request({
      url: qqMapApi,
      header: {
        'Content-Type': 'application/json'
      },
      data: {},
      method:'GET',
      success: (res) => {
          <!-- 获取的返回值数据就是传入的经纬度提取到的 各级地理数据信息 -->
        if (res.statusCode == 200 && res.data.status == 0) {
          // 从返回值中提取需要的业务地理信息数据 国家、省、市、县区、街道
          // that.setData({ nation: res.data.result.address_component.nation });
          // that.setData({ province: res.data.result.address_component.province });
          that.setData({ city: res.data.result.address_component.city });
          // that.setData({ district: res.data.result.address_component.district });
          // that.setData({ street: res.data.result.address_component.street });
          wx.request({
            url: "http://wthrcdn.etouch.cn/weather_mini",
            data: {city: res.data.result.address_component.city},
            // data: {city: '昆明'},
            method: 'GET',
            success: res => {
              var low = res.data.data.forecast[0].low.split(' ', 2)[1]
              var high = res.data.data.forecast[0].high.split(' ', 2)[1]
              var skyTitle = low + '~' + high + ' ' + res.data.data.forecast[0].type
              var skyType = res.data.data.forecast[0].type
              this.setData({
                skyType: skyType,
                skyTitle: skyTitle
              })
              console.log(skyTitle)
            }
          })
        }
      }
    })
  },

最后得到所需要的位置信息