Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/398.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,我将上述内容保存在一个单独的文件中,用于引用我的app.js文件 var cityMarkers = [ { id: "bliss", name: "Principality of Bliss", icon: cityIcon, coords: [-90.19, -76.90] }, { id: "cantonia", name: "Grand City of Cantonia

我将上述内容保存在一个单独的文件中,用于引用我的app.js文件

var cityMarkers = [
    {
        id: "bliss",
        name: "Principality of Bliss",
        icon: cityIcon,
        coords: [-90.19, -76.90]
    },
    {
        id: "cantonia",
        name: "Grand City of Cantonia",
        icon: cityIcon,
        coords: [-39.513421, -69.09375]
    },
    {
        id: "mithril",
        name: "Grand City of Mithril ",
        icon: cityIcon,
        coords: [42, -102.5]
    }];
cityMarkers.forEach(功能(项目){
var marker=L.marker(item.coords,{icon:item.icon});
marker.bindTooltip(“+item.name+”,{permanent:true,偏移量:
[60, 0]});
这将生成标记和其他属性,但不会将它们放在地图上。数组处理将它们放在地图上的操作,因此这对我真正尝试做的事情没有多大帮助

这是一张基于传单库的地图。我正试图用id为每个城市分配一个变量。然后,在标记完成并附加到它们的变量后,我想用这些名称制作一个数组,作为数据层。我承认我在这里有点不知所措。任何指导都将不胜感激。我链接了文档如果有人需要,请在下面单击


我确实研究了这个问题,但是我找不到任何结果来回答我想问的问题。我更喜欢轻推而不是简单的回答。我不知道如何实例化变量并将它们绑定到标记上。谢谢您的时间。

我想您可以尝试添加
。addTo(map)

cityMarkers.forEach(功能(项目){
var marker=L.marker(item.coords,{icon:item.icon});
标记
.bindTooltip(“+item.name+”,{permanent:true,偏移量:[60,0]})
.addTo(地图);

将标记添加到
L.layerGroup
中,而不是直接添加到地图中。您可以将
layerGroup
添加到地图中,然后随意将其删除

cityMarkers.forEach(function(item) {
  var marker = L.marker(item.coords, {icon : item.icon});
  marker
    .bindTooltip("<b>" + item.name + "<b>", {permanent: true, offset: [60, 0]})
    .addTo(map);
var lg=new L.layerGroup();
城市营销人员。forEach(功能(项目){
var marker=L.marker(item.coords,{icon:item.icon});
marker.bindTooltip(“+item.name+”,{permanent:true,偏移量:
[60, 0]})
.addTo(lg)});
lg.addTo(map);//添加图层将地图分组
lg.removefom(地图);//从地图中删除图层组

是的,它会将它们添加到地图中,但它们不会位于允许它们切换的图层中。params permanent true使它们留在地图上。您能检查一下吗params permanent是一个工具提示选项。它使文本永久,而不是标记。为清晰起见,permanent和offset是选项对象的一部分,选项对象是地图的一部分bindtoolTip()方法。不管怎样,谢谢你花时间回复。嗯。这起作用了。不过我很困惑。我认为这不起作用,因为数组值没有唯一的变量。我认为在引用要包含在控件中的层以切换所有城市时,这一点很重要。所以,我想我真正的问题是数组值如何获得assi当从循环中创建它们时是否出现异常?顺便感谢您的回答!
cityMarkers.forEach(function(item) {
  var marker = L.marker(item.coords, {icon : item.icon});
  marker
    .bindTooltip("<b>" + item.name + "<b>", {permanent: true, offset: [60, 0]})
    .addTo(map);
var lg = new L.layerGroup();
cityMarkers.forEach(function(item) {
      var marker = L.marker(item.coords, {icon : item.icon});
      marker.bindTooltip("<b>" + item.name + "<b>", {permanent: true, offset: 
  [60, 0]})
  .addTo(lg)});

lg.addTo(map);       // Add the layerGroup the map
lg.removeFrom(map);  // Remove the layerGroup from the map