自学微信小程序的第十三天
DAY13
1、使用map组件在页面中创建地图后,若想在JS文件中对地图进行控制,需要通过地图API来完成。先通过wx.createMapContext()方法创建MapContext(Map上下文)实例,然后通过该实例的相关方法来操作map组件。
const mapCtx=wx.createMapContext(‘myMap’)
表52:MapContext实例的常用方法
方法 | 说明 |
getCenterLocation() | 获取当前地图中心的经纬度,返回GCJ-02坐标 |
moveToLaction() | 将地图中心移至当前定位点 |
表53:getCenterLocation()方法的常用选项
选项 | 类型 | 说明 |
iconPath | string | 图标路径,支持网络路径、本地路径、代码包路径 |
success | function | 接口调用成功的回调函数,通过其参数可以获取longitude(经度)和latitude(纬度) |
fail | function | 接口调用失败的回调函数 |
complete | function | 接口调用结束的回调函数 |
表54:moveToLocation()方法的常用选项
选项 | 类型 | 说明 |
longitude | number | 经度 |
latitude | number | 纬度 |
success | function | 接口调用成功的回调函数 |
fail | function | 接口调用失败的回调函数 |
complete | function | 接口调用结束的回调函数 |
2、如何使用MapContext实例获取当前地图中心的经纬度?
mapCtx.getCenterLocation({ const latitude=res.latitude console.log(longitude,latitude) } }) |