【问题标题】:get Div Ids for leaflet获取传单的 Div Id
【发布时间】:2014-09-05 21:44:19
【问题描述】:

我想在一页上有多个 mapquest 传单地图,但是在传单中,您必须像这样使用 div 中给出的 id 创建地图

     <div  style="color:black ;" id="map"></div>

然后

      plugin.Map =  new L.Map('map', {//<the id is used here
                layers : [mapLayer],
                center: new L.LatLng(39.828127,-98.579404),
                boxZoom: true,
                zoom: 5
            });

但是我想知道如果我有多个 div 是否可以获取 id,例如:

    <div  style="color:black; " id="map"></div>
    <div  style="color:black ;" id="map2"></div>

然后我就可以打电话了

   L.Map('map'...//where this is a possible array or something...

我不能只将 id 放入自己的原因是我创建了一个模板,我想这样做而不用硬编码任何东西......

【问题讨论】:

    标签: javascript html leaflet


    【解决方案1】:

    给他们类名class="maps",然后使用jquery

    <div  style="color:black" data-id="map1" class="maps"></div>
    <div  style="color:black" data-id="map2" class="maps"></div>
    <div  style="color:black" data-id="map3" class="maps"></div>
    <div  style="color:black" data-id="map4" class="maps"></div>
    
    <script>
        $().ready(function() {
            $(".maps").each(function(){
                var mapID = $(this).data('id');
    
                // do something
    
                 plugin.Map = new L.Map(mapID, {//<the id is used here
                     layers : [mapLayer],
                     center: new L.LatLng(39.828127,-98.579404),
                     boxZoom: true,
                     zoom: 5
                 });
            });
        );
    </script>
    

    一些资源:

    jquery get all form elements: input, textarea & select

    http://api.jquery.com/jquery.each/

    【讨论】:

    • 而不是添加一个类,如果它们都在一个名为 maps 的 div 中,您可以在上述解决方案中使用 $('#maps div')
    • 这也是一种可能。
    • 这似乎工作,但现在,两个地图都不正确,当我更改一个图层时,另一个变灰,是否可以像这样实现两个地图?跨度>
    • 我的猜测是,您可能需要为每个地图使用单独的 API,这样您就不会在不同的地图中破坏 API 数据。因此,当不仅生成 MapID 时,我还会为每个地图生成完全独立的 API。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-10
    • 1970-01-01
    • 2021-05-01
    相关资源
    最近更新 更多