简体   繁体   English

如何正确添加图层控件?

[英]How do you properly add a layer control?

I seem to be having quite the issue adding a layer control menu to my page.我似乎在向我的页面添加图层控制菜单时遇到了相当大的问题。 I thought I had the code correct but every time I try to add it, it doesn't show.我以为我的代码是正确的,但每次我尝试添加它时,它都没有显示。 What am I doing wrong?我究竟做错了什么?

L.geoJSON(cities).addTo(map); L.geoJSON(城市).addTo(地图);

  ```code```

  var baseMaps = {
      "States": statesData
  };

  var overlayMaps = {
      "cities": cities
  };

  L.control.layers(baseMaps, overlayMaps).addTo(map);

You have to create a Layergroup / GeoJsonlayer to add them to the controler.您必须创建一个 Layergroup / GeoJsonlayer 才能将它们添加到控制器中。 You can not add the plain geojson Object to the controler.您不能将普通的 geojson 对象添加到控制器。

var statesDataLayer = L.geoJSON(statesData); // Don't know if this is also geojson?
var citiesLayer = L.geoJSON(cities);

var baseMaps = {
      "States": statesDataLayer // Don't know if this is also geojson?
  };

  var overlayMaps = {
      "cities": citiesLayer
  };

  L.control.layers(baseMaps, overlayMaps).addTo(map);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM