如何在传单中的标记下方添加文本?

Jac*_*ake 4 html javascript css marker leaflet

我在传单地图上创建了一些标记,但唯一的问题是我不知道如何在每个标记下方添加文本,而且我不是在谈论弹出窗口。我的意思是标记下方的文本必须是永久的。例如,印度标记下方的文字写着“印度”。我有一个我现在正在做的事情的例子:在此输入图像描述

var southWest = L.latLng(-89.98155760646617, -180), northEast = L.latLng(89.99346179538875, 180);
var bounds = L.latLngBounds(southWest, northEast);
var mymap = L.map('map', {
    center: [20.594, 78.962],
    maxBounds: bounds, // set max bounds for the world map
    zoom: 4, // default zoom level when the web is initiated
    zoomSnap: 0.25, // map's zoom level must be in multiple of this
    zoomDelta: 0.25, // controls how much the map's zoom level will change after a zoom
    minZoom: 3.25, // min zoom level the user can zoom out
    maxZoom: 6, // max zoom level the user can zoom in
    zoomControl: true, // allow zooming
});
mymap.zoomControl.setPosition('topright'); // set + and - zoom buttons to top right corner .setOpacity('0.4')
L.DomUtil.setOpacity(mymap.zoomControl.getContainer(), 0.2); // set opacity of zoom buttons
var MapAttribution = '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors &copy; <a href="https://carto.com/attributions">CARTO</a>';
var DarkMatterUrl = 'https://{s}.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}{r}.png'; // For dark theme map
var PositronUrl = 'https://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}{r}.png'; // For light theme map
var tiles = L.tileLayer(DarkMatterUrl, { MapAttribution }).addTo(mymap); 
// Array of marker coordinates
var markers = [
    {
        coords:[4.21, 101.97],
        country:'Malaysia',
        label:'Malaysia',
    },
    {
        coords:[20.594, 78.962],
        country:'India',
        label:'India',
    },
    {
        coords:[35.861, 104.195],
        country:'China',
        label:'China',
    },
    {
        coords:[23.421, 53.8478],
        country:'UAE',
        label:'UAE',
    },
    {
        coords:[23.6978, 120.9605],
        country:'Taiwan',
        label:'Taiwan',
    },
    {
        coords:[0.7892, 113.9213],
        country:'Indonesia',
        label:'Indonesia',
    },
];
// Edit marker icons
var myIcon = L.icon({
    iconUrl: 'yellowcircle.png',
    iconSize: [40, 40], // size of the icon
    // iconAnchor: [],
    // popupAnchor: [],
});
// Loop through markers
for(var i = 0; i<markers.length; i++){
    addMarker(markers[i]);
}
// To add the marker coordinates
function addMarker(props){
    var marker = L.marker(props.coords, {icon: myIcon}).bindTooltip(props.country).addTo(mymap);
    marker.on('mouseover', function(e){
        marker.openPopup(); 
    });
    marker.on('mouseout', function(e){
        marker.closePopup();
    });
}
// For GeoJSON features
var StringLines = [{
    "type": "Feature",
    "geometry": {
        "type": "LineString",
        "coordinates": [
            [113.9213, 0.7892],[101.97, 4.21],[120.9605, 23.6978] // From Indonesia to Malaysia to Taiwan
            ]
    }
}, {
    "type": "Feature",
    "geometry": {
        "type": "LineString",
        "coordinates": [
            [53.8478, 23.421],[101.97, 4.21],[104.195, 35.861],[78.962, 20.594] // From UAE to Malaysia to China to India
            ]
    }
}];
// Edits for LineStrings
var LineStringColor = {
    "color": "#ff7800",
    "weight": 5,
    "opacity": 0.65
}
L.geoJSON(StringLines, {style: LineStringColor}).addTo(mymap); // add geoJSON features to the map
Run Code Online (Sandbox Code Playgroud)

Iva*_*hez 8

您可能不想为每个数据点显示一个而是两个标记,其中一个带有 来L.DivIcon仅显示文本,例如:

var coords = L.latLng(0, 0);
L.marker(coords).addTo(map);
L.marker(coords, {
  icon: L.divIcon({
      html: "Null Island",
      className: 'text-below-marker',
    })
}).addTo(map);
Run Code Online (Sandbox Code Playgroud)

添加一些与 相关的 CSSDivIconclassName使DivIcon更大,例如:

.text-below-marker {
  min-width: 100px;
  left: -50px;
  text-align: center;
  min-height: 1.2em;
  border: 1px solid red;
}
Run Code Online (Sandbox Code Playgroud)

在一个看起来像这样的工作示例中:

分割图标的例子

您可能还希望将两个标记分组到 a 中,L.LayerGroup将它们视为一个实体(例如用于L.Control.Layers切换其可见性的目的)。使用您自己的 HTML/CSS 技能以及Leaflet 的iconAnchor和选项进一步调整它。iconSizeDivIcon


或者,您可以将永久元素L.Tooltip与自定义 CSS 类一起使用,使其具有透明背景,例如:

marker.bindTooltip('Null Island', {
  permanent: true, 
  direction : 'bottom',
  className: 'transparent-tooltip',
  offset: [0, -8]
});
Run Code Online (Sandbox Code Playgroud)

CSS 会是这样的

.transparent-tooltip {
  background: transparent;
  border: 1px solid red;
  box-shadow: none;
}

.transparent-tooltip::before {
  border: none;
}
Run Code Online (Sandbox Code Playgroud)

工作示例看起来像......

工具提示示例

再次,使用工具提示选项和您自己的 CSS 进一步调整它。