【Mapbox GL JS 入门】(4)Markers and controls 标记和控件

简介

可以添加到地图的用户界面元素。此部分位于地图的画布元素之外。

分类

AttributionControl

该控件显示地图的属性信息

名称类型默认值描述
compactboolean?若true,则鼠标悬停时将强制显示一个简洁版的属性信息,若false,则强制显示完整属性控件。默认为一个响应属性控件,当地图宽度低于640像素的时候该控件会折叠起来
customAttribution(string or Array string )?额外的地图属性显示内容
const map = new mapboxgl.Map({attributionControl: false})
    .addControl(new mapboxgl.AttributionControl({
        customAttribution: 'Map design by me'
    }));

FullscreenControl

该控件包含用于在全屏模式下切换地图的按钮

名称类型默认值描述
containerHTMLElement?全屏显示的元素,默认是地图容器元素
map.addControl(new mapboxgl.FullscreenControl({container: document.querySelector('body')}));

GeolocateControl

该控件提供了可以使用浏览器的地理定位API在地图上定位用户的按钮

名称类型默认值描述
fitBoundsOptionsObject{maxZoom:15}当地图平移并缩放到用户的位置时要使用的 fitBounds
geolocationObjectwindow.navigator.geolocation提供具有相同形状的对象来自定义
positionOptionsObject{enableHighAccuracy:false,timeout:6000}
showAccuracyCircleObjecttrue
showUserHeadingObjectfalse
showUserLocationObjecttrue
trackUserLocationObjectfalse
map.addControl(new mapboxgl.GeolocateControl({
    positionOptions: {
        enableHighAccuracy: true
    },
    trackUserLocation: true,
    showUserHeading: true
}));

IControl

添加到地图的交互式控件的接口,控件必须实现onAdd和onRemove,并且必须拥有一个元素,该元素通常是div元素。要使用MapboxGLJS的默认控件样式,请将mapboxgl-ctrl类添加到控件的节点。

// Control implemented as ES6 class
class HelloWorldControl {
    onAdd(map) {
        this._map = map;
        this._container = document.createElement('div');
        this._container.className = 'mapboxgl-ctrl';
        this._container.textContent = 'Hello, world';
        return this._container;
    }

    onRemove() {
        this._container.parentNode.removeChild(this._container);
        this._map = undefined;
    }
}

Marker

创建标记组件。

名称类型默认值描述
anchorstring‘center’标记对齐方式的字符串,选项为“center”、“top”、“bottom”、“left”、“right”、“top-left”、“top-right”、“bottom-left和“bottom-right”
clickTolerancenumber0用户在标记上单击时可以移动鼠标指针的最大像素数,以便将其视为有效的单击(与标记拖动相反)。默认值是继承地图的clickTolerance
colorstring‘#3FB1CE’如果element没提供的话,默认标记颜色,淡蓝色
draggablebooleanfalse标记是否可以拖拽到地图的其他位置
elementHTMLElement?标记的DOM元素,默认设置为浅蓝色的水滴形SVG标记
occludedOpacitynumber0.2被三维地形遮挡的标记的不透明度
offsetPointLike?PointLike对象相对于元素中心的像素偏移量,负数表示左和上
pitchAlignmentstring‘auto’“map”将标记与地图平面对齐。“视口”将标记与视口平面对齐。“auto”自动匹配rotationAlignment的值。
rotationnumber0标记旋转角度(以°为单位),正值代表顺时针旋转
rotationAlignmentstring‘auto’标记旋转的对齐方式。“map”与地图平面对齐,与地图旋转时的基本方向一致。“viewport”与屏幕空间对齐。“horizon”根据最近的地平线对齐,在非地球投影上,它相当于“视口”。“auto”等同于“viewport”。
scalenumber1如果element没提供的话,默认标记尺寸,默认:41*27px
const marker = new mapboxgl.Marker()
    .setLngLat([30.5, 50.5])
    .addTo(map); // add the marker to the map

const marker = new mapboxgl.Marker().addTo(map);
marker.remove();

// Store the marker's longitude and latitude coordinates in a variable
const lngLat = marker.getLngLat();
// Print the marker's longitude and latitude values in the console
console.log(`Longitude: ${lngLat.lng}, Latitude: ${lngLat.lat}`);

const element = marker.getElement();

const marker = new mapboxgl.Marker()
    .setLngLat([0, 0])
    .setPopup(new mapboxgl.Popup().setHTML("<h1>Hello World!</h1>")) // add popup
    .addTo(map);

console.log(marker.getPopup()); // return the popup instance
const marker = new mapboxgl.Marker()
    .setLngLat([0, 0])
    .setPopup(new mapboxgl.Popup().setHTML("<h1>Hello World!</h1>"))
    .addTo(map);

marker.togglePopup(); // toggle popup open or closed

const offset = marker.getOffset();
marker.setOffset([0, 1]);

marker.setDraggable(true);
const isMarkerDraggable = marker.isDraggable();

marker.setRotation(45);
const rotation = marker.getRotation();

marker.setRotationAlignment('viewport');
const alignment = marker.getRotationAlignment();

marker.setPitchAlignment('map');
const alignment = marker.getPitchAlignment();

marker.setOccludedOpacity(0.3);
const opacity = marker.getOccludedOpacity();

NavigationControl

该控件显示地图的属性信息

名称类型默认值描述
showCompassbooleantrue若true,则包含指南针按钮
showZoombooleantrue若true,则包括放大和缩小按钮
visualizePitchbooleanfalse若true,则通过旋转罗盘的X轴来显示俯仰
const nav = new mapboxgl.NavigationControl({
    visualizePitch: true
});
map.addControl(nav, 'bottom-right');

Popup

该控件显示地图的属性信息

名称类型默认值描述
anchorstring?若true,则,若false,则
classNamestring?用空格分隔的CSS类名添加到弹出容器中
closeButtonbooleantrue若true,则关闭按钮将出现在弹出窗口的右上角
closeOnClickbooleantrue若true,则单击地图时,弹出窗口将关闭
closeOnMovebooleanfalse若true,则当地图移动时,弹出窗口将关闭
focusAfterOpenbooleantrue若true,则弹出窗口将尝试聚焦弹出窗口中的第一个可聚焦元素
maxWidthstring‘240px’设置弹出窗口最大宽度的CSS属性的字符串(例如,“300px”)。要确保弹出窗口的大小适合其内容,请将此属性设置为“none”
offset(number or PointLike or Object)?PointLike对象相对于元素中心的像素偏移量,负数表示左和上
const markerHeight = 50;
const markerRadius = 10;
const linearOffset = 25;
const popupOffsets = {
    'top': [0, 0],
    'top-left': [0, 0],
    'top-right': [0, 0],
    'bottom': [0, -markerHeight],
    'bottom-left': [linearOffset, (markerHeight - markerRadius + linearOffset) * -1],
    'bottom-right': [-linearOffset, (markerHeight - markerRadius + linearOffset) * -1],
    'left': [markerRadius, (markerHeight - markerRadius) * -1],
    'right': [-markerRadius, (markerHeight - markerRadius) * -1]
};
const popup = new mapboxgl.Popup({offset: popupOffsets, className: 'my-class'})
    .setLngLat(e.lngLat)
    .setHTML("<h1>Hello World!</h1>")
    .setMaxWidth("300px")
    .addTo(map);

ScaleControl

该控件显示地图的属性信息

名称类型默认值描述
maxWidthnumber‘100’缩放控件的最大长度(以像素为单位)
unitstring‘metric’距离单位:( ‘imperial’ , ‘metric’ or ‘nautical’ )
const scale = new mapboxgl.ScaleControl({
    maxWidth: 80,
    unit: 'imperial'
});
map.addControl(scale);

scale.setUnit('metric');

方法

addControl(control, position?)

向地图中添加一个IControl控件,调用control.onAdd(this)

// Add zoom and rotation controls to the map.
map.addControl(new mapboxgl.NavigationControl());

removeControl(control)

从地图中移除控件

// Define a new navigation control.
const navigation = new mapboxgl.NavigationControl();
// Add zoom and rotation controls to the map.
map.addControl(navigation);
// Remove zoom and rotation controls from the map.
map.removeControl(navigation);

hasControl(control)

检查某个控件是否在地图上

// Define a new navigation control.
const navigation = new mapboxgl.NavigationControl();
// Add zoom and rotation controls to the map.
map.addControl(navigation);
// Check that the navigation control exists on the map.
const added = map.hasControl(navigation);
// added === true

getContainer()

返回地图的HTML元素

const container = map.getContainer();

getCanvasContainer()

返回地图canvas元素的容器

const canvasContainer = map.getCanvasContainer();

getCanvas()

返回地图的canvas元素

const canvas = map.getCanvas();
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值