如何在Leaflet.js中添加多个标记?

问题描述 投票:0回答:1

我想在我的地图上添加多个标记以精确定位以下坐标。

  1. 11.8166° N, 122.0942° E
  2. 11.9804° N, 121.9189° E
  3. 10.7202° N, 122.5621° E
  4. 11.3889° N,122.6277° E
  5. 10.5929° 北纬,122.6325° 东经
javascript html css maps leaflet
1个回答
51
投票

你可以这样做:

希望对您有所帮助:)

var locations = [
  ["LOCATION_1", 11.8166, 122.0942],
  ["LOCATION_2", 11.9804, 121.9189],
  ["LOCATION_3", 10.7202, 122.5621],
  ["LOCATION_4", 11.3889, 122.6277],
  ["LOCATION_5", 10.5929, 122.6325]
];

var map = L.map('map').setView([11.206051, 122.447886], 8);
mapLink =
  '<a href="http://openstreetmap.org">OpenStreetMap</a>';
L.tileLayer(
  'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
    attribution: '&copy; ' + mapLink + ' Contributors',
    maxZoom: 18,
  }).addTo(map);

for (var i = 0; i < locations.length; i++) {
  marker = new L.marker([locations[i][1], locations[i][2]])
    .bindPopup(locations[i][0])
    .addTo(map);
}
#map {
  width: 600px;
  height: 400px;
}
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" />
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script>
<div id='map'></div>

© www.soinside.com 2019 - 2024. All rights reserved.