> 微信小程序开发教程手册 > 微信小程序API 位置

wx.getLocation(OBJECT)——查看最新版本

​微信小程序获取当前的地理位置、速度

OBJECT参数说明:

参数 类型 必填 说明
type String 默认为"wgs84"返回gps坐标,"gcj02"返回可用于wx.openLocation的坐标
success Function 成功获取地理位置的回调
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

success返回参数说明:

参数 说明
latitude 纬度,浮点数,范围为-90~90,负数表示南纬
longitude 经度,浮点数,范围为-180~180,负数表示西经
speed 速度,浮点数,单位m/s
accuracy 位置的精确度
success Function
fail Function
complete Function

示例代码:

wx.getLocation({
    type: 'wgs84',
    success: function (res) {
        var latitude = res.latitude; 
        var longitude = res.longitude; 
        var speed = res.speed; 
        var accuracy = res.accuracy; 
    }
});

wx.openLocation(OBJECT)——查看最新版本

微信小程序使用微信内置地图查看位置

OBEJCT参数说明:

参数 类型 必填 说明
latitude Float 纬度,范围为-90~90,负数表示南纬
longitude Float 经度,范围为-180~180,负数表示西经
scale INT 缩放比例,范围1~28,默认为28
infoUrl String 在查看位置界面底部显示的超链接,可点击跳转
name String 位置名
address String 地址的详细说明
success Function 接口调用成功的回调函数
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

示例代码:

wx.getLocation({
    type: 'gcj02', //返回可以用于wx.openLocation的经纬度
    success: function (res) {
        var latitude = res.latitude; 
        var longitude = res.longitude; 
        wx.openLocation({
          latitude:latitude,
          longitude:logitude,
          scale:1
        })
    }
});