从两个不同的AJAX请求添加Leaflet标记

时间:2015-05-18 09:23:07

标签: javascript jquery ajax leaflet

我有两个不同的JSON响应,从2个AJAX调用中获取。第一个将数据正确地绘制到Leaflet Map。但是,第二个不起作用。

<script type="text/javascript">
        $(document).ready(function () {
      var map = L.map('map').setView([22.6059596, 88.4277967], 13);

      L.tileLayer('http://api.tiles.mapbox.com/v4/abhishekdepro.li9a02j1/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoiYWJoaXNoZWtkZXBybyIsImEiOiI3ZXg1UzlFIn0.d584w0ILalmPRzbJwgxdTw#4', {
          attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://mapbox.com">Mapbox</a>',
          maxZoom: 18
      }).addTo(map);
      if (localStorage.getItem("user_contact") === null) {

      } else {
          document.getElementById("user").innerHTML = "Hello, " + localStorage.getItem("user_contact");
          document.getElementById("_login").innerHTML = "Logout";
      }
      //var marker = L.marker([51.5, -0.09]).addTo(map);
      //marker.bindPopup("<b>Hello world!</b><br>I am a popup.").openPopup();

      $.ajax({
          type: 'GET',
          dataType: 'json',
          url: 'https://bloodplus.herokuapp.com/geopoints',
          success: function (data) {
              var obj = data;
              var totalLocations = obj.length;

              var array = [];
              var greenIcon = L.icon({
                  iconUrl: '/images/icon.png',

              });
              array = obj.bloods;
              //alert(array.length);

              for (var i = 0; i < array.length; i++) {

                  marker = new L.marker([parseFloat(array[i].tweet.lat), parseFloat(array[i].tweet.lon)], {
                      icon: greenIcon
                  })
                      .bindPopup(array[i].tweet.group + ', Contact: ' + array[i].tweet.contact).addTo(map);



              }
              another();
          }
      });

  });

  function another() {
      $.ajax({
          type: 'GET',
          dataType: 'json',
          url: 'https://bloodplus.herokuapp.com/geopoints',
          success: function (data) {
              var obj = data;
              var totalLocations = obj.length;
              //document.getElementById('name').value = parseFloat(obj.bloods[0].tweet.lat);

              var array = [];
              var greenIcon = L.icon({
                  iconUrl: '/images/icon.png',

              });
              array = obj.bloods;
              //alert(array.length);

              for (var i = 0; i < array.length; i++) {

                  marker = new L.marker([parseFloat(array[i].tweet.lat), parseFloat(array[i].tweet.lon)], {
                      icon: greenIcon
                  })
                      .bindPopup(array[i].tweet.group + ', Contact: ' + array[i].tweet.contact).addTo(map);



              }
          }
      });


  }
</script>   

但是当我在此之后发出另一个AJAX请求时。标记没有加载,我得到一个错误:

  

TypeError:undefined不是函数(评估't.addLayer(this)')

1 个答案:

答案 0 :(得分:2)

您需要使用new L.MarkerL.marker,而不是两者的组合 - new L.marker。请参阅http://leafletjs.com/reference.html#class中的“类别工厂”部分。