簡體   English   中英

傳單如何在同一圖層組中組合圖標和圓形標記

[英]Leaflet how to combine icons and circle markers in the same layergroup

我有一堆坐標要添加到傳單地圖上。 標有值“ home”的坐標將獲得類似於home的svg圖標,並存儲在homeIcons變量中。 所有其他坐標將獲得一個圓形標記,並存儲在名為circles變量circles 我當前的方法只能成功地將circles變成正確的傳單對象,而不能成功地將homeIcons 因此,當我使用L.layerGroup()將它們組合成一個圖層組時,來自'homeIcons'的對象就被丟棄了。 如果您看到我的代碼的問題在哪里,請告訴我。 謝謝!

    var circles = [];
    var homeIcons = [];

    var markerOptions = {
      radius      : 10,
      fillColor   : 'yellow',
      color       : 'null',
      weight      : 1,
      opacity     : 1,
      fillOpacity : 0.8
    };

    var homeIcon = L.icon({
      iconUrl: 'public/imgs/home_icon.svg',
      iconSize     : [30, 30],
      iconAnchor   : [8, 18],
      popupAnchor  : [0, -15],
    });

    var homeMarker = L.marker([null, null], {icon: homeIcon, opacity: 1});

    for (var i=0; i<data.length; i++) {
      var pois = JSON.parse(data[i].POIs);

      for (var n=0; n<pois.length; n++) {
        homeMarker = {latlng:[pois[n].lat, pois[n].lng], name: data[i].Name}; 

        if (pois[n].poiTag == "home") {
        homeIcons.push(L.marker(homeMarker));
        } else {
        circles.push(L.circleMarker([pois[n].lat, pois[n].lng], markerOptions, {name: data[i].Name}));
        }
      }
    }

    console.log(circles);    // Outputs an array of leaflet objects
    console.log(homeIcons);  // Not outputing an array of objects with similar properties as leaflet objects

    var allLayers = L.layerGroup(circles, homeIcons);  // this is supposed to add both circles to homeIcons to the layergroup

    console.log(allLayers);  // By here, all the homeIcons have disappeared.

    cb(allLayers);

  }

您是否檢查過您的“ poiTag”是否正確/拼寫正確? 您能否提供數據示例

if (pois[n].poiTag == "home") {
  homeIcons.push(L.marker(homeMarker));
 } else {
  circles.push(L.circleMarker([pois[n].lat, pois[n].lng], markerOptions, {name: data[i].Name}));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM