2013-10-07 75 views
2

我有一個GeoRSS源,我試圖用jQuery解析來創建一個geoJSON數組,然後我可以使用Leaflet將其添加到MapBox地圖中。使用jQuery從GeoRSS循環GeoJSON項目

我想我設法把GeoRSS變成GeoJSON好了,但是我似乎不知道如何循環每個項目,然後我可以將它添加到我的地圖中。如果我取出循環部分,我會在我的地圖上繪製一個點 - 最近一次來自RSS提要的條目。

任何指針將非常感激!

這裏是我運行代碼:

$(document).ready(function(){ 
$.get('http://shifting-sands.com/feed/', function(rssdata) { 
var $xml = $(rssdata); 
$xml.find("title").each(function() { 
    var $this = $(this), 
     item = { 
      title: $this.find("title").text(), 
      link: $this.find("link").text(), 
      description: $this.find("description").text(), 
      pubDate: $this.find("pubDate").text(), 
      latitude: $this.find("lat").text(), 
      longitude: $this.find("long").text()     
    } 

功能displayPosts(rssdata){$ 。每個(rssdata.rss.channel.item,功能(I,項目){

   //Read in the lat and long of each photo and stores it in a variable. 
       lat = item.latitude; 
       long = item.longitude; 
       title = item.title; 
       clickurl = item.link; 
       //Get the url for the image. 
       var htmlString = '<a href="' + clickurl + '">' + title + '</a>';       
       var contentString = '<div id="content">' + htmlString + '</div>'; 

       //Create a new marker position using the Leaflet API. 
       var rssmarker = L.marker([lat, long]).addTo(map); 

       //Create a new info window using the Google Maps API 

       rssmarker.bindPopup(contentString).openPopup(); 
     }); 
    } 

    }); 
}); 

});

回答

2

創建GeoJSON的要素集合:

var featurecollection = { type: 'FeatureCollection', features: [] };

而且在每次循環迭代,推功能到它。

featurecollection.features.push({ 
    type: 'Feature', 
    properties: {}, // any properties you want in the feature 
    geometry: [long, lat] 
});