简体   繁体   English

我正在使用 jQuery 在传单地图中插入地标。 但是当我尝试通过单击链接插入地标时,它给了我错误

[英]I'm inserting placemarks in leaflet map with jQuery. But when I try to insert the placemarks by clicking a link it gives me error

The error I get is "Uncaught Error: Map container is already initialized."我得到的错误是“未捕获的错误:地图容器已经初始化。”
The function placethemarks() loads data by using another function pullJson() that divides the data I want to visualize on the map by clicking a corresponding link.函数 placethemarks() 使用另一个函数 pullJson() 加载数据,该函数通过单击相应的链接来划分我想要在地图上可视化的数据。 How can I solve this error ?我该如何解决这个错误?

$(window).load(function(){                 
         var element = $('#mapPage');         
         element.height(element.height() - 42);             
         var map = L.map('map').setView([44.493889, 11.342778], 13);          
         L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png',               
         attributionControl = false).addTo(map);         

    });


function placethemarks(){
 var jsondata = pullJsonData();
         var marker_art = new Array();
         var marker_museum = new Array();   

        for(var a in jsondata){
            var tipocat = jsondata[a].type; 

            if(tipocat=='artwork') {
                storeCoordinate(jsondata[a].geometry.coordinates[0], jsondata[a].geometry.coordinates[1], marker_art);
            }
            else if(tipocat == 'arts_centre'||tipocat == 'museum' ) {
                storeCoordinate(jsondata[a].geometry.coordinates[0], jsondata[a].geometry.coordinates[1], marker_museum);
            }
         }

        var marker_array = []; 
         for (var i = 0; i < marker_art.length; i++) {
          var x = marker_art[i].x;
          var y = marker_art[i].y;
          var arr = [y,x];
          marker_array.push(arr);

         }

         for(var i = 0; i < marker_array.length-1; i++){
                   new L.marker(marker_array[i]).addTo(map);
         }

      }

  • Art艺术
  • Museums博物馆
  • I solved finally the problem by simply adding to the code the Leaflet map I had to add.我最终通过简单地将我必须添加的 Leaflet 地图添加到代码中解决了这个问题。 Now when i click the button I see the placemarks added to the map .现在,当我单击按钮时,我看到地标已添加到地图中。 Hese is the code : Hese是代码:

      <div id = "butt">
            <button type="button" onClick = placeTheMarks()>Place The Marks!</button>
            </div>
    
         <script>
             function placeTheMarks(){       
    
               // I take the data from another page 
               var json = JSON.parse(window.sessionStorage.getItem('response'));
    
                 var marker_art = new Array();
                 var marker_museum = new Array();   
                 for(var a in json){
                    var tipocat = json[a].type; 
    
                    if(tipocat=='artwork') {
                        storeCoordinate(json[a].geometry.coordinates[0], json[a].geometry.coordinates[1], marker_art);
                    }
                    else if(tipocat == 'arts_centre' || tipocat == 'museum' ) {
                        storeCoordinate(json[a].geometry.coordinates[0], json[a].geometry.coordinates[1], marker_museum);
                    }
                 }
                var marker_array = []; 
                 for (var i = 0; i < marker_art.length; i++) {
                  var x = marker_art[i].x;
                  var y = marker_art[i].y;
                  var arr = [y,x];
                  marker_array.push(arr);
    
                 }
    
                 for(var i = 0; i < marker_array.length-1; i++){
                           new L.marker(marker_array[i]).addTo(map);
                 }
             }
    
                var map = L.map('map').setView([44.7215826, 10.624086], 12);
    
                 L.tileLayer("http://a.tile.openstreetmap.org/{z}/{x}/{y}.png", {
                    attribution: 'Map data&copy; OpenStreetMap contributors'
                   }).addTo(map);
    
                </script>
    

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

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