微信小程序8 位置与设备api

位置

wx.getLocation(OBJECT)

获取当前的地理位置、速度。当用户离开小程序后,此接口无法调用;当用户点击“显示在聊天顶部”时,此接口可继续调用

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

wx.chooseLocation(OBJECT)

打开地图选择位置。

wx.openLocation(OBJECT)

​使用微信内置地图查看位置。

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


wx.createMapContext(mapId)

创建并返回 map 上下文 mapContext 对象

<!-- map.wxml -->
<map id="myMap" show-location />

<button type="primary" bindtap="getCenterLocation">获取位置</button>
<button type="primary" bindtap="moveToLocation">移动位置</button>
<button type="primary" bindtap="translateMarker">移动标注</button>
<button type="primary" bindtap="includePoints">缩放视野展示所有经纬度</button>
// map.js
Page({
  onReady: function (e) {
    // 使用 wx.createMapContext 获取 map 上下文
    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()
  },
  translateMarker: function() {
    this.mapCtx.translateMarker({
      markerId: 0,
      autoRotate: true,
      duration: 1000,
      destination: {
        latitude:23.10229,
        longitude:113.3345211,
      },
      animationEnd() {
        console.log('animation end')
      }
    })
  },
  includePoints: function() {
    this.mapCtx.includePoints({
      padding: [10],
      points: [{
        latitude:23.10229,
        longitude:113.3345211,
      }, {
        latitude:23.00229,
        longitude:113.3345211,
      }]
    })
  }
})

方法参数说明最低版本
getCenterLocationOBJECT获取当前地图中心的经纬度,返回的是 gcj02 坐标系,可以用于 wx.openLocation 
moveToLocation将地图中心移动到当前定位点,需要配合map组件的show-location使用 
translateMarkerOBJECT平移marker,带动画1.2.0
includePointsOBJECT缩放视野展示所有经纬度1.2.0
getRegionOBJECT获取当前地图的视野范围1.4.0
getScaleOBJECT获取当前地图的缩放级别

translateMarker 的 OBJECT 参数列表

参数类型必填说明
markerIdNumber指定marker
destinationObject指定marker移动到的目标点
autoRotateBoolean移动过程中是否自动旋转marker
rotateNumbermarker的旋转角度
durationNumber动画持续时长,默认值1000ms,平移与旋转分别计算
animationEndFunction动画结束回调函数
failFunction接口调用失败的回调函数

includePoints 的 OBJECT 参数列表

参数类型必填说明
pointsArray要显示在可视区域内的坐标点列表,[{latitude, longitude}]
paddingArray坐标点形成的矩形边缘到地图边缘的距离,单位像素。格式为[上,右,下,左],安卓上只能识别数组第一项,上下左右的padding一致。开发者工具暂不支持padding参数。


设备

wx.getSystemInfo(OBJECT)

获取系统信息。

wx.getSystemInfo({
  success: function(res) {
    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.platform)
  }
})
completeFunction接口调用结束的回调函数(调用成功、失败都会执行)

success回调参数说明:

参数说明最低版本
brand手机品牌1.5.0
model手机型号 
pixelRatio设备像素比 
screenWidth屏幕宽度1.1.0
screenHeight屏幕高度1.1.0
windowWidth可使用窗口宽度 
windowHeight可使用窗口高度 
language微信设置的语言 
version微信版本号 
system操作系统版本 
platform客户端平台 
fontSizeSetting用户字体大小设置。以“我-设置-通用-字体大小”中的设置为准,单位:px1.5.0
SDKVersion客户端基础库版本1.1.0

wx.getSystemInfoSync()

获取系统信息同步接口

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.platform)
} catch (e) {
  // Do something when catch error
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值