Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将默认的传单控件替换为单个按钮集_Javascript_Leaflet - Fatal编程技术网

Javascript 如何将默认的传单控件替换为单个按钮集

Javascript 如何将默认的传单控件替换为单个按钮集,javascript,leaflet,Javascript,Leaflet,我的地图上有三个基本图层和三个带标记的图层。现在我用standart传单控件在它们之间切换。我需要将其替换为地图底部每个图层的一组单独按钮。如下图所示: 有人能告诉我如何替换默认的传单控件吗 L.control.layers(baseLayers, overlays).addTo(map); 到自定义按钮集? 这是我的小提琴: 我还需要在左上角的选择器中添加一个初始值。JSON中的第一个值现在就出现了,但我需要将其替换为类似“选择一个位置”的内容。要创建一个模型中的中心控件,您可以修改@gh

我的地图上有三个基本图层和三个带标记的图层。现在我用standart传单控件在它们之间切换。我需要将其替换为地图底部每个图层的一组单独按钮。如下图所示:

有人能告诉我如何替换默认的传单控件吗

L.control.layers(baseLayers, overlays).addTo(map);
到自定义按钮集? 这是我的小提琴:


我还需要在左上角的选择器中添加一个初始值。JSON中的第一个值现在就出现了,但我需要将其替换为类似“选择一个位置”的内容。

要创建一个模型中的中心控件,您可以修改@ghybs为前面的问题发布的内容。基本上,您可以创建一个css类来将控件集中在地图窗格中:

.leaflet-horizontalcenter {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  padding-top: 10px;
}

.leaflet-horizontalcenter .leaflet-control {
  margin-bottom: 10px;
}
然后创建一个新占位符,并将该类附加到地图容器:

function addControlPlaceholders(map) {
  var corners = map._controlCorners,
    l = 'leaflet-',
    container = map._controlContainer;

  function createCorner(vSide, hSide) {
    var className = l + vSide + ' ' + l + hSide;
    corners[vSide + hSide] = L.DomUtil.create('div', className, container);
  }
  createCorner('horizontalcenter', 'bottom');
}
addControlPlaceholders(map);
您可以使用任何您喜欢的方法来创建按钮,但是显示了一种将单选按钮样式设置为矩形框的相当简单的方法。使用这些按钮创建自定义控件的过程如下:

var buttonBar = L.control({
  position: 'horizontalcenterbottom'
});

buttonBar.onAdd = function(map) {
  //create div container for control
  var div = L.DomUtil.create('div', 'myButtonBar');
  //prevent mouse events from propagating through to the map
  L.DomEvent.disableClickPropagation(div);
  //create custom radio buttons
  div.innerHTML = '<div class="switch-field noselect"><div class="switch-title">MAPS</div>'
    + '<input type="radio" id="switch_map1" name="map_switch" checked/>'
    + '<label for="switch_map1">Map 1</label>'
    + '<input type="radio" id="switch_map2" name="map_switch" />'
    + '<label for="switch_map2">Map 2</label>'
    + '<input type="radio" id="switch_map3" name="map_switch" />'
    + '<label for="switch_map3">Map 3</label></div>'
    + '<div class="switch-field noselect">'
    + '<input type="radio" id="switch_marker1" name="marker_switch" checked/>'
    + '<label for="switch_marker1">Marker 1</label>'
    + '<input type="radio" id="switch_marker2" name="marker_switch" />'
    + '<label for="switch_marker2">Marker 2</label>'
    + '<input type="radio" id="switch_marker3" name="marker_switch" />'
    + '<label for="switch_marker3">Marker 3</label>'
    + '<div class="switch-title">MARKERS</div></div>';
  return div;
};

buttonBar.addTo(map);
最后,要在单击按钮时更改图层,请附加一些事件侦听器。由于您已经在使用jQuery,我们将只使用它:

$("#switch_map1").click(function() {
  switchLayer(baseLayers, "Map 1");
});
$("#switch_map2").click(function() {
  switchLayer(baseLayers, "Map 2");
});
$("#switch_map3").click(function() {
  switchLayer(baseLayers, "Map 3");
});
$("#switch_marker1").click(function() {
  switchLayer(overlays, "Marker 1");
});
$("#switch_marker2").click(function() {
  switchLayer(overlays, "Marker 2");
});
$("#switch_marker3").click(function() {
  switchLayer(overlays, "Marker 3");
});
其中,
switchLayer
是一个函数,它从
baseLayers
overlays
对象根据其键获取图层,然后将其添加到地图并删除其他图层:

function switchLayer(collection, layerKey) {
  if (layerKey in collection) {
    $.each(collection, function(key, layer) {
      if (key === layerKey) {
        if (!map.hasLayer(layer)) {
          map.addLayer(layer);
        }
      } else if (map.hasLayer(layer)) {
        map.removeLayer(layer);
      }
    });
  } else {
    console.log('There is no layer key by the name "' + layerKey + '" in the specified object.');
  }
}
以下是所有这些共同作用的结果:


要创建模型中的中心控件,您可以修改@ghybs发布的前一个问题。基本上,您可以创建一个css类来将控件集中在地图窗格中:

.leaflet-horizontalcenter {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  padding-top: 10px;
}

.leaflet-horizontalcenter .leaflet-control {
  margin-bottom: 10px;
}
然后创建一个新占位符,并将该类附加到地图容器:

function addControlPlaceholders(map) {
  var corners = map._controlCorners,
    l = 'leaflet-',
    container = map._controlContainer;

  function createCorner(vSide, hSide) {
    var className = l + vSide + ' ' + l + hSide;
    corners[vSide + hSide] = L.DomUtil.create('div', className, container);
  }
  createCorner('horizontalcenter', 'bottom');
}
addControlPlaceholders(map);
您可以使用任何您喜欢的方法来创建按钮,但是显示了一种将单选按钮样式设置为矩形框的相当简单的方法。使用这些按钮创建自定义控件的过程如下:

var buttonBar = L.control({
  position: 'horizontalcenterbottom'
});

buttonBar.onAdd = function(map) {
  //create div container for control
  var div = L.DomUtil.create('div', 'myButtonBar');
  //prevent mouse events from propagating through to the map
  L.DomEvent.disableClickPropagation(div);
  //create custom radio buttons
  div.innerHTML = '<div class="switch-field noselect"><div class="switch-title">MAPS</div>'
    + '<input type="radio" id="switch_map1" name="map_switch" checked/>'
    + '<label for="switch_map1">Map 1</label>'
    + '<input type="radio" id="switch_map2" name="map_switch" />'
    + '<label for="switch_map2">Map 2</label>'
    + '<input type="radio" id="switch_map3" name="map_switch" />'
    + '<label for="switch_map3">Map 3</label></div>'
    + '<div class="switch-field noselect">'
    + '<input type="radio" id="switch_marker1" name="marker_switch" checked/>'
    + '<label for="switch_marker1">Marker 1</label>'
    + '<input type="radio" id="switch_marker2" name="marker_switch" />'
    + '<label for="switch_marker2">Marker 2</label>'
    + '<input type="radio" id="switch_marker3" name="marker_switch" />'
    + '<label for="switch_marker3">Marker 3</label>'
    + '<div class="switch-title">MARKERS</div></div>';
  return div;
};

buttonBar.addTo(map);
最后,要在单击按钮时更改图层,请附加一些事件侦听器。由于您已经在使用jQuery,我们将只使用它:

$("#switch_map1").click(function() {
  switchLayer(baseLayers, "Map 1");
});
$("#switch_map2").click(function() {
  switchLayer(baseLayers, "Map 2");
});
$("#switch_map3").click(function() {
  switchLayer(baseLayers, "Map 3");
});
$("#switch_marker1").click(function() {
  switchLayer(overlays, "Marker 1");
});
$("#switch_marker2").click(function() {
  switchLayer(overlays, "Marker 2");
});
$("#switch_marker3").click(function() {
  switchLayer(overlays, "Marker 3");
});
其中,
switchLayer
是一个函数,它从
baseLayers
overlays
对象根据其键获取图层,然后将其添加到地图并删除其他图层:

function switchLayer(collection, layerKey) {
  if (layerKey in collection) {
    $.each(collection, function(key, layer) {
      if (key === layerKey) {
        if (!map.hasLayer(layer)) {
          map.addLayer(layer);
        }
      } else if (map.hasLayer(layer)) {
        map.removeLayer(layer);
      }
    });
  } else {
    console.log('There is no layer key by the name "' + layerKey + '" in the specified object.');
  }
}
以下是所有这些共同作用的结果:


单击按钮时,您可能只需使用
map.addLayer
map.removeLayer
。至于“左上角的选择器”,这听起来应该是一个不同的问题,对当前代码有更多的解释。单击按钮时,您可能只需要使用
map.addLayer
map.removeLayer
。至于“左上角的选择器”,这听起来应该是一个不同的问题,对当前代码有更多的解释。