#位置信息API
>获取位置、选择位置、打开位置
>wx.getLocation(OBJECT)
>获取当前位置,包括当前位置的地理坐标、速度
>用户离开小程序后,此接口无法调用
属性 | 类型 | 必填 | 说明 |
type | string | 否 | wgs84 返回 gps 坐标,gcj02 返回可用于 wx.openLocation 的坐标 |
altitude | string | 否 | 传入 true 会返回高度信息,由于获取高度需要较高精确度,会减慢接口返回速度 |
success | function | 否 | 接口调用成功的回调函数,参数:纬度、经度、速度、位置的精确度 |
fail | function | 否 | 接口调用失败的回调函数 |
complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
示例代码:
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打开地图来选择位置
属性 | 类型 | 必填 | 说明 |
success | function | 否 | 接口调用成功的回调函数,参数:纬度、经度、速度、位置的精确度 |
fail | function | 否 | 接口调用失败的回调函数 |
complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
> wx.openLocation(OBJECT)
>使用该接口可以使用微信内置地图查看位
属性 | 类型 | 必填 | 说明 |
latitude | number | 是 | 纬度,范围为-90~90,负数表示南纬。使用 gcj02 国测局坐 标系 |
longitude | number | 是 | 经度,范围为-180~180,负数表示西经。使用 gcj02 国测局 坐标系 |
scale | number | 否 | 缩放比例,范围5~18 |
name | string | 否 | 位置名 |
address | string | 否 | 地址的详细说明 |
success | function | 否 | 接口调用成功的回调函数,参数:纬度、经度、速度、位置的精确度 |
fail | function | 否 | 接口调用失败的回调函数 |
complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
示例代码:
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使用
属性 | 类型 | 必填 | 说明 |
success | function | 否 | 接口调用成功的回调函数,参数:纬度、经度、速度、位置的精确度 |
fail | function | 否 | 接口调用失败的回调函数 |
complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
示例代码:
//.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)
属性 | 类型 | 必填 | 说明 |
success | function | 否 | 接口调用成功的回调函数,参数:纬度、经度、速度、位置的精确度 |
fail | function | 否 | 接口调用失败的回调函数 |
complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
属性 | 类型 | 说明 |
model | string | 设备型号 |
pixelRatio | number | 设备像素比 |
screenWidth | number | 屏幕宽度,单位px |
screenHeight | number | 屏幕高度,单位px |
windowWidth | number | 可使用窗口宽度,单位px |
windowHeight | number | 可使用窗口高度,单位px |
language | string | 微信设置的语言 |
version | string | 微信版本号 |
system | string | 操作系统及版本 |
platform | string | 客户端平台 |
>同步获取系统信息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
属性 | 类型 | 必填 | 说明 |
success | function | 否 | 接口调用成功的回调函数,参数:纬度、经度、速度、位置的精确度 |
fail | function | 否 | 接口调用失败的回调函数 |
complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
Page({
onload: function () {
wx.getNetworkType({
success: function(res) {
var networkType=res.networkType;
console.log("网络类型=" + networkType);
}
})
}
})
>加速度计
>监听加速度数据:
>wx.onAccelerometerChange(CallBack)
>监听加速度计
>频率为5次/秒
属性 | 类型 | 说明 |
x | number | X 轴 |
y | number | Y轴 |
z | number | Z轴 |
>开始监听加速度数据
>wx.startAccelerometer(OBJECT)
属性 | 类型 | 必填 | 说明 |
interval | string | 否 | 监听加速度数据回调函数的执行频率 |
success | function | 否 | 接口调用成功的回调函数,参数:纬度、经度、速度、位置的精确度 |
fail | function | 否 | 接口调用失败的回调函数 |
complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
> 停止监听加速度数据
>wx.stopAccelerometer(OBJECT)
属性 | 类型 | 必填 | 说明 |
success | function | 否 | 接口调用成功的回调函数,参数:纬度、经度、速度、位置的精确度 |
fail | function | 否 | 接口调用失败的回调函数 |
complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
>罗盘
>监听罗盘数据:
>wx.onCompassChange(CallBack)
>频率为5次/秒
属性 | 类型 | 说明 |
direction | Number | 面对的方向度数 |
>开始监听罗盘数据:
>wx.startCompass(OBJECT)
>停止监听罗盘数据:
>wx.stopCompass(OBJECT)
...持续学习完善中。
#学无止境#