Keyboard shortcuts

Press ← or → to navigate between chapters

Press ? to show this help

Press Esc to hide this help

MapContext

MapContext 实例,可通过 mc.createMapContext获取。

MapContext通过 id 跟一个 map 组件绑定,操作对应的 map组件。

方法

MapContext.getCenterLocation(Object object)

获取当前地图中心的经纬度。返回的坐标系参考 map 组件类型说明。

Object object 参数
属性类型默认值必填说明
longitudeNumber是经度
latitudeNumber是纬度
successFunction否接口调用成功的回调函数
failFunction否接口调用失败的回调函数
completeFunction否接口调用结束的回调函数(调用成功、失败都会执行)

MapContext.setLocMarkerIcon(Object object)

设置定位点图标,支持网络路径、本地路径、代码包路径

Object object 参数
属性类型默认值必填说明
iconPathString是图标路径,支持网络路径、本地路径、代码包路径
successFunction否接口调用成功的回调函数
failFunction否接口调用失败的回调函数
completeFunction否接口调用结束的回调函数(调用成功、失败都会执行)

MapContext.moveToLocation(Object object)

将地图中心移置当前定位点,此时需设置地图组件 show-location 为true。

Object object 参数
属性类型默认值必填说明
longitudeNumber是经度
latitudeNumber是纬度
successFunction否接口调用成功的回调函数
failFunction否接口调用失败的回调函数
completeFunction否接口调用结束的回调函数(调用成功、失败都会执行)

MapContext.translateMarker(Object object)

平移marker。

Object object 参数
属性类型默认值必填说明
markerIdString是指定 marker
destinationObject是指定 marker 移动到的目标点,坐标格式 {longitude, latitude}
autoRotateBooleantrue否根据路径方向自动改变 marker 的旋转角度
durationNumber否平滑移动的时间
successFunction否接口调用成功的回调函数
failFunction否接口调用失败的回调函数
completeFunction否接口调用结束的回调函数(调用成功、失败都会执行)

MapContext.moveAlong(Object object)

沿指定路径移动 marker,用于轨迹回放等场景。动画完成时触发回调事件,若动画进行中,对同一 marker 再次调用 moveAlong 方法,前一次的动画将被打断。

Object object 参数
属性类型默认值必填说明
markerIdString是指定 marker
path[Object]是移动路径的坐标数组 [{longitude, latitude}]
rotateNumber否marker 的旋转角度
successFunction否接口调用成功的回调函数
failFunction否接口调用失败的回调函数
completeFunction否接口调用结束的回调函数(调用成功、失败都会执行)

MapContext.includePoints(Object object)

缩放视野展示所有经纬度

Object object 参数
属性类型默认值必填说明
points[Object]是移动路径的坐标数组 [{longitude, latitude}]
padding[Number]否坐标点形成的矩形边缘到地图边缘的距离,单位像素。格式为[上,右,下,左],安卓上只能识别数组第一项,上下左右的padding一致。开发者工具暂不支持padding参数。
successFunction否接口调用成功的回调函数
failFunction否接口调用失败的回调函数
completeFunction否接口调用结束的回调函数(调用成功、失败都会执行)

MapContext.getRegion(Object object)

获取当前地图的视野范围

Object object 参数
属性类型默认值必填说明
successFunction否接口调用成功的回调函数
failFunction否接口调用失败的回调函数
completeFunction否接口调用结束的回调函数(调用成功、失败都会执行)

Object.success(Object res)

属性类型说明
southwestObject西南角经纬度, 格式{longitude, latitude}
northeastObject东北角经纬度, 格式{longitude, latitude}

MapContext.getRotate(Object object)

获取当前地图的旋转角

Object object 参数
属性类型默认值必填说明
successFunction否接口调用成功的回调函数
failFunction否接口调用失败的回调函数
completeFunction否接口调用结束的回调函数(调用成功、失败都会执行)

Object.success(Object res)

属性类型说明
rotateNumber旋转角

MapContext.getSkew(Object object)

获取当前地图的倾斜角

Object object 参数
属性类型默认值必填说明
successFunction否接口调用成功的回调函数
failFunction否接口调用失败的回调函数
completeFunction否接口调用结束的回调函数(调用成功、失败都会执行)

Object.success(Object res)

属性类型说明
skewNumber倾斜角

MapContext.getScale(Object object)

获取当前地图的缩放级别

Object object 参数
属性类型默认值必填说明
successFunction否接口调用成功的回调函数
failFunction否接口调用失败的回调函数
completeFunction否接口调用结束的回调函数(调用成功、失败都会执行)

Object.success(Object res)

属性类型说明
scaleNumber缩放值

MapContext.setCenterOffset(Object object)

设置地图中心点偏移,向后向下为增长,屏幕比例范围(0.25~0.75),默认偏移为[0.5, 0.5]

Object object 参数
属性类型默认值必填说明
offset[Number]是偏移量,两位数组 如 [0.5, 0.5]
successFunction否接口调用成功的回调函数
failFunction否接口调用失败的回调函数
completeFunction否接口调用结束的回调函数(调用成功、失败都会执行)

MapContext.removeCustomLayer(Object object)

移除个性化图层。

Object object 参数
属性类型默认值必填说明
layerIdString是个性化图层id
successFunction否接口调用成功的回调函数
failFunction否接口调用失败的回调函数
completeFunction否接口调用结束的回调函数(调用成功、失败都会执行)

MapContext.addCustomLayer(Object object)

添加个性化图层。(暂只支持腾讯地图)

Object object 参数
属性类型默认值必填说明
layerIdString是个性化图层id
successFunction否接口调用成功的回调函数
failFunction否接口调用失败的回调函数
completeFunction否接口调用结束的回调函数(调用成功、失败都会执行)

MapContext.addGroundOverlay(Object object)

创建自定义图片图层,图片会随着地图缩放而缩放。

Object object 参数
属性类型默认值必填说明
idString是图片图层 id
srcString是图片路径,支持网络图片、临时路径、代码包路径
boundsObject是图片覆盖的经纬度范围
visibleBoolean否是否可见
zIndexNumber1否图层绘制顺序
opacityNumber1否图层透明度
successFunction否接口调用成功的回调函数
failFunction否接口调用失败的回调函数
completeFunction否接口调用结束的回调函数(调用成功、失败都会执行)

Object.bounds 格式

属性类型说明
southwestObject西南角经纬度, 格式{longitude, latitude}
northeastObject东北角经纬度, 格式{longitude, latitude}

MapContext.updateGroundOverlay(Object object)

更新自定义图片图层。

Object object 参数
属性类型默认值必填说明
idString是图片图层 id
srcString是图片路径,支持网络图片、临时路径、代码包路径
boundsObject是图片覆盖的经纬度范围
visibleBoolean否是否可见
zIndexNumber1否图层绘制顺序
opacityNumber1否图层透明度
successFunction否接口调用成功的回调函数
failFunction否接口调用失败的回调函数
completeFunction否接口调用结束的回调函数(调用成功、失败都会执行)

MapContext.removeGroundOverlay(Object object)

移除自定义图片图层。

Object object 参数
属性类型默认值必填说明
idString是图片图层 id
successFunction否接口调用成功的回调函数
failFunction否接口调用失败的回调函数
completeFunction否接口调用结束的回调函数(调用成功、失败都会执行)

MapContext.toScreenLocation(Object object)

获取经纬度对应的屏幕坐标,坐标原点为地图左上角。

Object object 参数
属性类型默认值必填说明
longitudeNumber是经度
latitudeNumber是纬度
successFunction否接口调用成功的回调函数
failFunction否接口调用失败的回调函数
completeFunction否接口调用结束的回调函数(调用成功、失败都会执行)

Object.success(Object res)

属性类型说明
xNumberx 坐标值
yNumbery 坐标值

MapContext.fromScreenLocation(Object object)

获取屏幕上的点对应的经纬度,坐标原点为地图左上角。

Object object 参数
属性类型默认值必填说明
xNumber是x 坐标值
yNumber是y 坐标值
successFunction否接口调用成功的回调函数
failFunction否接口调用失败的回调函数
completeFunction否接口调用结束的回调函数(调用成功、失败都会执行)

Object.success(Object res)

属性类型说明
longitudeNumber经度
latitudeNumber纬度

MapContext.openMapApp(Object object)

拉起地图APP选择导航。

Object object 参数
属性类型默认值必填说明
longitudeNumber是目的地经度
latitudeNumber是目的地纬度
destinationString否目的地名称
successFunction否接口调用成功的回调函数
failFunction否接口调用失败的回调函数
completeFunction否接口调用结束的回调函数(调用成功、失败都会执行)

MapContext.addMarkers(Object object)

添加 marker。

Object object 参数
属性类型默认值必填说明
markersNumber是同传入 map 组件的 marker 属性
clearBooleanfalse否是否先清空地图上所有 marker
successFunction否接口调用成功的回调函数
failFunction否接口调用失败的回调函数
completeFunction否接口调用结束的回调函数(调用成功、失败都会执行)

MapContext.removeMarkers(Object object)

移除 marker。

Object object 参数
属性类型默认值必填说明
markerIds[String]是marker 的 id 集合。
successFunction否接口调用成功的回调函数
failFunction否接口调用失败的回调函数
completeFunction否接口调用结束的回调函数(调用成功、失败都会执行)

MapContext.initMarkerCluster(Object object)

初始化点聚合的配置,未调用时采用默认配置。

Object object 参数
属性类型默认值必填说明
enableDefaultStyleBooleantrue否启用默认的聚合样式
zoomOnClickBooleantrue否点击已经聚合的标记点时是否实现聚合分离
gridSizeNumber60否聚合算法的可聚合距离,即距离小于该值的点会聚合至一起,以像素为单位
successFunction否接口调用成功的回调函数
failFunction否接口调用失败的回调函数
completeFunction否接口调用结束的回调函数(调用成功、失败都会执行)

MapContext.on(String event, Function callback)

监听地图事件。

参数
参数类型默认值必填说明
eventString是事件名 markerClusterCreate或markerClusterClick
callbackFunction是事件回调

markerClusterCreate 事件回调参数

参数类型说明
clusters[ClusterInfo]聚合簇数据数组

markerClusterClick 事件回调参数

参数类型说明
clusterClusterInfo聚合簇数据

ClusterInfo 结构

参数类型说明
clusterIdNumber聚合簇的 id
centerLatLng聚合簇的坐标
markerIds[Number]该聚合簇内的点标记数据数组

示例代码:

const map = mc.createMapContext('mapId', this);
//获取中心点坐标
map.getCenterLocation({
    success: function(res){
        console.log(res.longitude, res.latitude);
    }
})

//获取视野范围
map.getRegion({
  success: function(res){
    console.log(res.southwest.longitude, res.southwest.latitude);
    console.log(res.northeast.longitude, res.northeast.latitude);
  }
})

//添加图片图层
map.addGroundOverlay({
  id: 1,
  src: '/assets/images/logo.png',
  bounds:{
    southwest: {longitude:116.380338, latitude: 39.92235},
    northeast: {longitude:116.414977, latitude: 39.947246}
  }
})

//添加点
var markers = [];
for (var i = 0; i < 10; i++) {
  this.data.markerId++;
  markers.push({
    id: this.data.markerId,
    clusterId: 'clusterId', 
    joinCluster: true,
    latitude: Math.random() * 30 + 21, 
    longitude: Math.random() * 40 + 85
  });
}
map.addMarkers({
  markers: markers,
  clear: false,//false 不清除已有点
  complete: this.setCallback
})

//聚合
map.initMarkerCluster({
  enableDefaultStyle: false,
  zoomOnClick: true,
  gridSize: 10,
  complete: this.setCallback
})
map.on('markerClusterCreate', function(res){
  //新增的聚合点
  console.log(res.clusters);
});
map.on('markerClusterClick', function(res){
  //点击的聚合点
  console.log(res.cluster);
});