2015-10-22 16 views
1

我目前正試圖在Mapbox地圖上反應性地顯示標記。我的方法是觀察一個集合,同時創建一個GeoJSON對象。但是,該特定對象中的更改不反映在地圖上。流星:帶映射箱的反應性標記

var featureArray = []; 
CollectionName.find({}).observe({ 
    added: function(item) { 
     featureArray.push({ 
     type: "Feature", 
     geometry: { 
      type: "Point", 
      coordinates: [+item.location.coordinates[0], +item.location.coordinates[1]] 
     }, 
     properties: { 
      _id: item._id, 
     } 
     }); 
    }, 
    changedAt: function(newDocument, oldDocument, atIndex) { 
    //.. 
    }, 
    removed: function(oldDocument, atIndex) { 
    //.. 
    } 
}); 

var CollectionGeoJSON = { 
    'type': 'FeatureCollection', 
    'features': testmarkers 
}; 

var markers = L.mapbox.featureLayer().setGeoJSON(CollectionGeoJSON); 

// add cluster markers and add them to map 

我的想法是在這裏手動添加/刪除/修改客戶端上的標誌物(如更改同步到服務器反正),但也沒有成功因爲我不知道如何使用做Mapbox API。

任何提示,非常感謝。

回答

1

我創建了一個meteorpad示例,顯示了這一點。

通過在onRendered回調中調用template.autorun來創建反應性。該template.autorun回調由任何改動的Cities.find()結果觸發,並更新.setGeoJSON

this.autorun(function() { 
    if (Mapbox.loaded()) { 
    geojson = Cities.find().fetch() 
    view.featureLayer.setGeoJSON(geojson); 
    } 
}); 

地圖在這個例子中的城市集合的內容都已經被傳遞到.setGeoJSON正確的格式,但如果您更喜歡可以擁有不同的Colleciton架構,並在template.autorun回調中以此格式創建列表。

+0

謝謝你的幫助!我相應地改變了我的集合模式,並且做到了這一點。我已經在onRendered回調中使用了template.autorun;轉換似乎是問題。 – leandermelms