将 Leaflet divIcon 标记与动态内容居中

Rob*_*iet 3 css leaflet

我创建了一个简单的传单地图,其中包含一些国家的标记。目前,所有标记都不是以中间点为中心,而是以左上角为中心。

这是我现在的代码:

const countries = [
  [52.32, 5.55, "🇳🇱 Netherlands"],
  [50.64, 4.67, "🇧🇪 Belgium"],
  [51, 10, "🇩🇪 Germany"],
  [47, 2, "🇫🇷 France"],
  [42.56, 1.56, "🇦🇩 Andorra"],
  [40.2, -3.5, "🇪🇸 Spain"],
  [38.7, -9.18, "🇵🇹 Portugal"],
  [52, 19, "🇵🇱 Poland"],
  [50, 16, "🇨🇿 Czechia"],
  [46.8, 8.23, "🇨🇭 Switzerland"]
];

let map = L.map("map", {
  worldCopyJump: true
}).setView({
  lat: 50,
  lon: 10
}, 5);

L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", {
  minZoom: 2,
  maxZoom: 10,
  attribution: "&copy; <a href=\"https://openstreetmap.org/copyright\">OpenStreetMap</a>"
}).addTo(map);

L.control.scale().addTo(map);

const regions = L.featureGroup().addTo(map);

countries.forEach((country) => {
  L.marker([country[0], country[1]], {
    icon: L.divIcon({
      iconSize: "auto",
      html: "<b>" + country[2] + "</b>"
    })
  }).addTo(regions);
})

map.addLayer(regions);
Run Code Online (Sandbox Code Playgroud)
html,
body {
  height: 100%;
  padding: 0;
  margin: 0;
}

#map {
  width: 100%;
  height: 100%;
}

.leaflet-div-icon {
  border: 1px solid #666;
  border-radius: 3px;
  padding: 3px;
  text-align: center;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js"></script>
<link href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css" rel="stylesheet" />
<div id="map"></div>
Run Code Online (Sandbox Code Playgroud)

我的预期结果是,所有类名为“leaflet-div-icon”的 div 元素的宽度的一半位于左侧,高度的一半位于上方。例如看安道尔,目前只有标记的左上角点在安道尔境内,其余的都在安道尔之外。我希望标记的中点位于安道尔。

我尝试添加以下代码,但这会将所有标记移动到错误的位置:

.leaflet-div-icon {
    position: relative;
    top: -50%;
    left: -50%;
}
Run Code Online (Sandbox Code Playgroud)

Kos*_*ery 5

您可以覆盖默认.leaflet-div-icon样式,然后将其子样式设置b为您想要的外观和放置位置:

const countries = [
  [52.32, 5.55, "&#127475;&#127473;&nbsp;Netherlands"],
  [50.64, 4.67, "&#127463;&#127466;&nbsp;Belgium"],
  [51, 10, "&#127465;&#127466;&nbsp;Germany"],
  [47, 2, "&#127467;&#127479;&nbsp;France"],
  [42.56, 1.56, "&#127462;&#127465;&nbsp;Andorra"],
  [40.2, -3.5, "&#127466;&#127480;&nbsp;Spain"],
  [38.7, -9.18, "&#127477;&#127481;&nbsp;Portugal"],
  [52, 19, "&#127477;&#127473;&nbsp;Poland"],
  [50, 16, "&#127464;&#127487;&nbsp;Czechia"],
  [46.8, 8.23, "&#127464;&#127469;&nbsp;Switzerland"]
];

let map = L.map("map", {
  worldCopyJump: true
}).setView({
  lat: 50,
  lon: 10
}, 5);

L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", {
  minZoom: 2,
  maxZoom: 10,
  attribution: "&copy; <a href=\"https://openstreetmap.org/copyright\">OpenStreetMap</a>"
}).addTo(map);

L.control.scale().addTo(map);

const regions = L.featureGroup().addTo(map);

countries.forEach((country) => {
  L.marker([country[0], country[1]], {
    icon: L.divIcon({
      iconSize: "auto",
      html: "<b>" + country[2] + "</b>"
    })
  }).addTo(regions);
})

map.addLayer(regions);
Run Code Online (Sandbox Code Playgroud)
html,
body {
  height: 100%;
  padding: 0;
  margin: 0;
}

#map {
  width: 100%;
  height: 100%;
}

#map .leaflet-div-icon {
  width:0;
  height:0;
  border: 0;
  padding: 0;
}

#map .leaflet-div-icon b {
  display:inline-block;
  padding: 3px;
  border: 1px solid #666;
  border-radius: 3px;
  background:#fff;
  transform:translate(-50%, -50%);
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js"></script>
<link href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css" rel="stylesheet" />
<div id="map"></div>
Run Code Online (Sandbox Code Playgroud)