微信小程序API_位置信息API_设备应用API

#位置信息API

        >获取位置、选择位置、打开位置

                >wx.getLocation(OBJECT)

                        >获取当前位置,包括当前位置的地理坐标、速度

                        >用户离开小程序后,此接口无法调用             

属性类型必填说明
typestringwgs84 返回 gps 坐标,gcj02 返回可用于 wx.openLocation 的坐标
altitudestring传入 true 会返回高度信息,由于获取高度需要较高精确度,会减慢接口返回速度
successfunction接口调用成功的回调函数,参数:纬度、经度、速度、位置的精确度
failfunction接口调用失败的回调函数
completefunction接口调用结束的回调函数(调用成功、失败都会执行)

        示例代码:

Page({
    onLoad:function(){
        wx.getLocation({
            type: ‘wgs84’,
            success:function(res){
                var latitude=res.latitude;
                console.log(“纬度=”+latitude);
                var longitude=res.longitude;
                console.log(“经度=”+latitude);
                var speed=res.speed;
                console.log(“速度”+speed);
                var accuracy=res.accuracy;
                console.log(“精确度”+accuracy);
            }
        })
    }
})

                >wx.chooseLocation(OBJECT)

                        >使用wx.chooseLocation打开地图来选择位置

属性类型必填说明
successfunction接口调用成功的回调函数,参数:纬度、经度、速度、位置的精确度
failfunction接口调用失败的回调函数
completefunction接口调用结束的回调函数(调用成功、失败都会执行)

        > wx.openLocation(OBJECT)

                >使用该接口可以使用微信内置地图查看位

属性类型必填说明
latitudenumber纬度,范围为-90~90,负数表示南纬。使用 gcj02 国测局坐 标系
longitudenumber经度,范围为-180~180,负数表示西经。使用 gcj02 国测局 坐标系
scalenumber缩放比例,范围5~18
namestring位置名
addressstring地址的详细说明
successfunction接口调用成功的回调函数,参数:纬度、经度、速度、位置的精确度
failfunction接口调用失败的回调函数
completefunction接口调用结束的回调函数(调用成功、失败都会执行)

        示例代码:

Page({
    onLoad:function(){
        wx.getLocation({
            type:”gcj02”,
            success:function(res) {
                var latitude=res.latitude
                var longitude=res.longitude
                wx.openLocation({
                    latitude: latitude,
                    longitude:longitude,
                    scale: 28
                })
            }
        })
    }
})

        >地图组件控制

                >地图组件控制是用来创建并返回map上下文 mapContext对象的

                >它有两种方法:

                        >getCenterLocation :获取当前地图中心的经纬度,可以用于 wx.openLocation

                        >moveToLocation :将地图中心移动到当前定位点,需要配合map组件的 show-location使用

属性类型必填说明
successfunction接口调用成功的回调函数,参数:纬度、经度、速度、位置的精确度
failfunction接口调用失败的回调函数
completefunction接口调用结束的回调函数(调用成功、失败都会执行)

        示例代码:

//.wxml

<map id="myMap" show-location></map>
<button type="primary" bindtap="getCenterLocation">获取位置</button>
<button type="primary" bindtap="moveToLocation">移动位置</button>

//.js

Page({
    onLoad:function(e){ this.mapCtx=wx.createMapContext('myMap')
    },
    getCenterLocation:function() {
        this.mapCtx.getCenterLocation({
            success:function(res){
                console.log(res.longitude)
                console.log(res.latitude)
            }
        })
    },
    moveToLocation:function(){
        this.mapCtx.moveToLocation()
    }
})

#设备应用API

        >获得系统信息

                >异步获取系统信息wx.getSystemInfo(OBJECT)

属性类型必填说明
successfunction接口调用成功的回调函数,参数:纬度、经度、速度、位置的精确度
failfunction接口调用失败的回调函数
completefunction接口调用结束的回调函数(调用成功、失败都会执行)
success参数说明
属性类型说明
modelstring设备型号
pixelRationumber设备像素比
screenWidthnumber屏幕宽度,单位px
screenHeightnumber屏幕高度,单位px
windowWidthnumber可使用窗口宽度,单位px
windowHeightnumber可使用窗口高度,单位px
languagestring微信设置的语言
versionstring微信版本号
systemstring操作系统及版本
platformstring客户端平台

                >同步获取系统信息wx.getSystemInfoSync(OBJECT)

                        >同步获取系统信息是没有参数的

Page({
    onload:function(){
        try{
            var res=wx.getSystemInfoSync()
            console.log("手机型号="+res.model)
            console.log("设备像素比="+res.pixelRatio)
            console.log("窗口宽度=" + res.windowWidth)
            console.log("窗口高度=" + res.windowHeight)
            console.log("微信设置的语言=" + res.language)
            console.log("微信版本号=" + res.version)
            console.log("操作系统版本=" + res.system)
            console.log("客户端平台=" + res.platform) 
            }catch(e){
                //出错时的处理过程
            }
    }
})

        >获取网络状态

                >微信小程序使用 wx.getNetworkType(OBJECT)来获取网络 类型,网络类型分为2G、3G、4G、Wifi

属性类型必填说明
successfunction接口调用成功的回调函数,参数:纬度、经度、速度、位置的精确度
failfunction接口调用失败的回调函数
completefunction接口调用结束的回调函数(调用成功、失败都会执行)
Page({
    onload: function () {
        wx.getNetworkType({
            success: function(res) {
                var networkType=res.networkType;
                console.log("网络类型=" + networkType);
            }
        })
    }
})

        >加速度计

                >监听加速度数据:

                        >wx.onAccelerometerChange(CallBack)

                        >监听加速度计

                        >频率为5次/秒

属性类型说明
xnumberX 轴
ynumberY轴
znumberZ轴

                >开始监听加速度数据

                        >wx.startAccelerometer(OBJECT)

属性类型必填说明
intervalstring监听加速度数据回调函数的执行频率
successfunction接口调用成功的回调函数,参数:纬度、经度、速度、位置的精确度
failfunction接口调用失败的回调函数
completefunction接口调用结束的回调函数(调用成功、失败都会执行)

                > 停止监听加速度数据

                        >wx.stopAccelerometer(OBJECT)

属性类型必填说明
successfunction接口调用成功的回调函数,参数:纬度、经度、速度、位置的精确度
failfunction接口调用失败的回调函数
completefunction接口调用结束的回调函数(调用成功、失败都会执行)

        >罗盘

                >监听罗盘数据:

                        >wx.onCompassChange(CallBack)

                        >频率为5次/秒

属性类型说明
directionNumber面对的方向度数

                 >开始监听罗盘数据:

                        >wx.startCompass(OBJECT)

                >停止监听罗盘数据:

                        >wx.stopCompass(OBJECT)

...持续学习完善中。


#学无止境#

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南枫知我意~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值