目录

自学微信小程序的第十三天

目录

自学微信小程序的第十三天

DAY13

1、使用map组件在页面中创建地图后,若想在JS文件中对地图进行控制,需要通过地图API来完成。先通过wx.createMapContext()方法创建MapContext(Map上下文)实例,然后通过该实例的相关方法来操作map组件。

const mapCtx=wx.createMapContext(‘myMap’)

表52:MapContext实例的常用方法

方法说明
getCenterLocation()获取当前地图中心的经纬度,返回GCJ-02坐标
moveToLaction()将地图中心移至当前定位点

表53:getCenterLocation()方法的常用选项

选项类型说明
iconPathstring图标路径,支持网络路径、本地路径、代码包路径
successfunction接口调用成功的回调函数,通过其参数可以获取longitude(经度)和latitude(纬度)
failfunction接口调用失败的回调函数
completefunction接口调用结束的回调函数

表54:moveToLocation()方法的常用选项

选项类型说明
longitudenumber经度
latitudenumber纬度
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数
completefunction接口调用结束的回调函数

2、如何使用MapContext实例获取当前地图中心的经纬度?

mapCtx.getCenterLocation({ success:res=>{ const longitude=res.longitude const latitude=res.latitude console.log(longitude,latitude) } })