查看“其他API”的源代码
←
其他API
跳转至:
导航
,
搜索
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看与复制此页面的源代码。
== 其他API == ==== wx.getLocation函数 ==== 作用:获取当前的地理位置、速度。当用户离开小程序后,此接口无法调用。 ====== 参数 ====== [[文件:属性1.png|700px]] ====== object.success 回调函数 ====== [[文件:success.png|700px]] ====== 代码示例 ====== //index.js Page({ data: { demo_la: 48.1545, demo_lo: 82.3555907, latitude: 0, longitude: 0, begin_la: 0, begin_lo: 0, interval: 0, speed: 0, accuracy: 0, markers: [{ iconPath: "../../imgs/pic.jpg", id: 0, latitude: 39.9205, longitude: 116.4605, width: 30, height: 30 }] }, openLoc() { this.mapCtx.includePoints({ padding: [10], points: [{ latitude: 23.10229, longitude: 113.3345211, }, { latitude: 23.00229, longitude: 113.3345211, }] }) }, geoInfo() { var that = this wx.getLocation({ type: 'gcj02', scale:18, success(res) { that.setData({ latitude: res.latitude, longitude: res.longitude, speed: res.speed, accuracy: res.accuracy, }) } }) console.log(this.data.latitude, this.data.longitude) }, startRun() { var that = this clearInterval(this.data.interval) wx.getLocation({ type: 'gcj02', success(res) { var la = res.latitude var lo = res.longitude that.setData({ begin_la: la, begin_lo: lo, 'markers[1]': { iconPath: "../../imgs/red.png", id: 1, latitude: la, longitude: lo, width: 20, height: 20 } }) } }) var interval = setInterval(() => { this.running() }, 1000) }, stopRun() { var that = this clearInterval(this.data.interval) wx.getLocation({ type: 'gcj02', success(res) { var la = res.latitude var lo = res.longitude that.setData({ latitude: la, longitude: lo, 'markers[2]': { iconPath: "../../imgs/yellow.png", id: 2, latitude: la, longitude: lo, width: 20, height: 20 } }) } }) console.log('开始位置', this.data.begin_la, this.data.begin_lo) console.log('结束位置', this.data.latitude, this.data.longitude) console.log(this.data) }, running() { this.data.latitude += 0.0002 this.data.longitude += 0.0002 }, center() { this.mapCtx.getCenterLocation({ success: function (res) { console.log(res.latitude,res.longitude) } }) }, trans() { this.mapCtx.translateMarker({ markerId: 0, autoRotate: false, duration: 3000, destination: { latitude: 40.30229, longitude: 116.7545211, }, animationEnd() { console.log('animation end') } }) }, onReady: function () { this.mapCtx = wx.createMapContext('myMap') }, }) //index.wxml <view class='top'> <button bindtap='geoInfo'>位置</button> <button bindtap='openLoc'>打开</button> <button bindtap='center'>定心</button> <button bindtap='trans'>动</button> </view> <map id='myMap' style="width:100%; height:350px" longitude='{{longitude}}' latitude='{{latitude}}' markers='{{markers}}' polyline="{{polyline}}" show-location subkey='IWLBZ-CK6R4-YXDUO-DQCXK-J5H4Z-DSBCI'> <cover-view calss='cover'> 经度:{{longitude}} 纬度:{{latitude}} 速度:{{speed}} </cover-view> </map> <button bindtap='startRun'>开始</button> <button bindtap='stopRun'>结束</button>
该页面使用的模板:
模板:Latitude
(
查看源代码
)
模板:Longitude
(
查看源代码
)
模板:Markers
(
查看源代码
)
模板:Polyline
(
查看源代码
)
模板:Speed
(
查看源代码
)
返回至
其他API
。
导航菜单
个人工具
登录
命名空间
页面
讨论
变种
视图
阅读
查看源代码
查看历史
更多
搜索
导航
首页
最近更改
随机页面
帮助
工具
链入页面
相关更改
特殊页面
页面信息