微信小程序使用获取经纬度和腾讯地图的使用

本文介绍了如何在微信小程序中获取用户经纬度,包括使用微信内置的定位功能以及集成腾讯地图进行地图显示和标记点、画圈的功能。

先看效果图

1、微信小程序获取经纬度

        1、获取接口权限 官方地址

        

        2、页面使用

    //打开提示框
    wx.showLoading({
      title: '正在定位中',
      mask: true
    });
    wx.getLocation({
      type: 'gcj02', //wgs84 返回 gps 坐标,gcj02 
      altitude: false, //传入 true 会返回高度信息,由于获取高度需要较高精确度,会减慢接
      isHighAccuracy: false, //开启高精度定位
      highAccuracyExpireTime: 3000, //高精度定位超时时间(ms),3000ms以上定位才有效果
      success(res) {
        //隐藏提示框
        wx.hideLoading();

        let location = {};
        location.horizontalAccuracy = res.horizontalAccuracy;
        location.latitude = parseFloat(res.latitude);
        location.longitude = parseFloat(res.longitude);
        //更新定位信息
        that.setData({
          horizontalAccuracy: res.horizontalAccuracy,
          latitude: parseFloat(res.latitude), //纬度,范围为 -90~90,负数表示南纬 
          longitude: parseFloat(res.longitude), //经度,范围为 -180~180,负数表示西经 
        })
        wx.setStorageSync('storage_location', location)
        // console.log('存储成功',location);
      },
      fail: function (res) {
        console.log(res)
        //隐藏提示框
        wx.hideLoading();
        wx.showToast({
          title: "定位失败"+res.errMsg,
          duration: 2000,
          icon: 'none'
        });
      }
    })

 2、小程序使用腾讯地图

        官方地址

<map id="myMap" style="width:100%; height:380rpx;" longitude="{{longitude}}" latitude="{{latitude}}" show-location="true" scale="16" markers="{{markers}}" circles="{{circles}}" enable-rotate="true">
地图标点
//地图画圈
    markers: [{
      id: 0,
      latitude: '',
      longitude: '',
      width: 32,
      height: 45
    }]
//id是唯一标识,经度维度控制标点位置,标记点可以有多个

 

地图画圈
circles: [{
      latitude: '',
      longitude: '',
      color: '#3197ed',
      fillColor: '#88bff388',
      radius: 40,
      strokeWidth: 1,
    }]
//经度维度控制圈的位置,圈可以有多个,radius控制圈的半径

 

微信小程序获取设备的经纬度并将其转换为对应的城市名称,可以通过以下步骤实现: ### 获取经纬度 首先需要使用 `wx.getLocation` 接口获取用户的经纬度信息。此接口返回的是用户当前的地理坐标(latitude longitude)[^1]。 ```javascript wx.getLocation({ type: 'gcj02', // 可选值:wgs84 返回 gps 坐标,gcj02 返回国测局坐标 success(res) { const latitude = res.latitude; const longitude = res.longitude; console.log(`Latitude: ${latitude}, Longitude: ${longitude}`); // 进行下一步操作,如逆地理编码 }, fail(err) { console.error('Failed to get location:', err); } }); ``` ### 经纬度转换为城市名 为了将获取到的经纬度转换为具体的城市名称,可以使用腾讯地图提供的逆地理编码服务。这一步骤不需要直接使用地图组件,而是通过调用腾讯地图的 Web API 来完成。 #### 引入腾讯地图 SDK 确保你已经在项目中引入了腾讯地图的 JavaScript SDK,并且配置好了相应的密钥(Key)[^2]。 ```javascript const QQMapWX = require('@/utils/wx_js/qqmap-wx-jssdk.min.js'); const qqmapsdk = new QQMapWX({ key: '你的腾讯地图API密钥' // 替换为你自己的密钥 }); ``` #### 调用逆地理编码接口 接下来,使用 `reverseGeocoder` 方法将经纬度转换为具体的地址信息,包括城市名称。 ```javascript function getLocationInfo(location) { return new Promise((resolve, reject) => { qqmapsdk.reverseGeocoder({ location: { latitude: location.latitude, longitude: location.longitude }, success(res) { resolve(res.result); }, fail(err) { console.error('Reverse geocoding failed:', err); reject(err); } }); }); } ``` #### 调用示例 最后,在获取经纬度后,调用 `getLocationInfo` 函数来获取详细地址信息,并从中提取城市名称。 ```javascript wx.getLocation({ type: 'gcj02', success: async (res) => { let result = await getLocationInfo(res); console.log("Address details:", result); const city = result.address_component.city; // 提取城市名称 console.log("City name:", city); }, fail(error) { console.error('Error getting location:', error); } }); ``` 通过上述步骤,可以在不依赖地图组件的情况下,成功获取用户的经纬度并将其转换为对应的城市名称。
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值