更改标记文本与单张地图按钮

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

我有一个标记,一个JSON文件生成,包含有数值的文本的地图。对于标记代我用的是Beaufify Markers Plugin。这是代码的一部分,我使用

var map = L.map('mapid').setView([43.08, 12.34], 9);

for ( var i=0; i < markers.length; ++i ) {
    options = {
      isAlphaNumericIcon: true,
      text: markers[i].temperatura,
      iconSize: [28, 28],
      borderColor: markers[i].color,
      backgroundColor: markers[i].color,
      innerIconAnchor: [0, 4],
      textColor: '#000;'
    };
    var popup = L.popup({
      pane: 'fixed',
      className: 'popup-fixed',
      autoPan: false,
    }).setContent('my html code');

    L.marker( [markers[i].lat, markers[i].lng], {
        icon: L.BeautifyIcon.icon(options),
        virtual: true
    })
    .bindPopup(popup)
    .addTo( map );
}

我想最好的和干净的方法,来改变选项可变文本(“标记[I] .temperatura”),与上一个按钮一个onclick动作。

javascript leaflet markers
1个回答
2
投票

要更新标记图标的文字:

1)保存标记插入全局变量,例如列表:

var markerObjects = [];
for (var i=0; i < markers.length; ++i ) {
    //...

    var markerObject = L.marker( [markers[i].lat, markers[i].lng], {
        icon: L.BeautifyIcon.icon(options),
        virtual: true
    })
    .bindPopup(popup)
    .addTo( map );
    markerObjects.push(markerObject);

}

2)一旦该按钮被点击时,通过下列方式更新Marker.setIcon method标记图标:

btn.onclick = () => {
    const selectedIndex = ...;  //get the selected index of marker 
    const selectedMarker = markerObjects[selectedIndex];
    let iconOptions = selectedMarker.options.icon.options; //get existing icon properties
    iconOptions.text = txtName.value; //update icon property
    selectedMarker.setIcon(L.BeautifyIcon.icon(iconOptions)); //update marker icon 
};

演示

const map = L.map("map").setView([53.339025, 18.065818], 4);

L.tileLayer("http://{s}.tile.osm.org/{z}/{x}/{y}.png", {
  attribution:
    '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);

const locations = [
  {
    name: "Oslo",
    lat: 59.923043,
    lng: 10.752839
  },
  {
    name: "Stockholm",
    lat: 59.339025,
    lng: 18.065818
  },
  {
    name: "Copenhagen",
    lat: 55.675507,
    lng: 12.574227
  },
  {
    name: "Berlin",
    lat: 52.521248,
    lng: 13.399038
  },
  {
    name: "Paris",
    lat: 48.856127,
    lng: 2.346525
  }
];

const markers = locations.map(location => {
  let options = {
    isAlphaNumericIcon: true,
    text: location.name,
    iconSize: [98, 28],
    textColor: "#000;"
  };
  let popup = L.popup({
    className: "popup-fixed",
    autoPan: false
  }).setContent(location.name);

  return L.marker([location.lat, location.lng], {
    icon: L.BeautifyIcon.icon(options),
    virtual: true
  })
    .bindPopup(popup)
    .addTo(map);
});

initControl();

function initControl() {
  

  let selectLocations = document.getElementById("selectLocations");
  let idx = 0;
  for (const marker of markers) {
    const option = document.createElement("option");
    option.value = idx;
    option.text = marker.options.icon.options.text;
    selectLocations.appendChild(option);
    idx++;
  }

  let txtName = document.getElementById("txtName");
  txtName.value = locations[selectLocations.selectedIndex].name;
  selectLocations.onchange = e => {
    txtName.value = markers[e.target.value].options.icon.options.text;
  };

  let btnUpdate = document.getElementById("btnUpdate");
  btnUpdate.onclick = () => {
    const selectedIndex = selectLocations.selectedIndex;
    const selectedMarker = markers[selectedIndex];
    let iconOptions = selectedMarker.options.icon.options; //get existing icon properties
    iconOptions.text = txtName.value; //update text property
    selectedMarker.setIcon(L.BeautifyIcon.icon(iconOptions)); //set icon 
  };
}
<link
  rel="stylesheet"
  href="https://unpkg.com/[email protected]/dist/leaflet.css"
/>
<link rel="stylesheet" href="style.css" />
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script>


<link
  rel="stylesheet"
  href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css"
/>
<link
  rel="stylesheet"
  href="https://marslan390.github.io/BeautifyMarker/leaflet-beautify-marker-icon.css"
/>

<script src="https://marslan390.github.io/BeautifyMarker/leaflet-beautify-marker-icon.js"></script>


<select id="selectLocations">
</select>
<input id="txtName" type="text"/>
<button id="btnUpdate">Update</button> 
<div id="map" style="height:410px; width:570px;"></div>
© www.soinside.com 2019 - 2024. All rights reserved.