[英]How can I add new markers to Google Map in Meteor?
I'm currently adding markers on a google map when I click on a given location. 当我点击给定位置时,我正在谷歌地图上添加标记。 The markers are currently saved both to a Collection - Markers - and in to a more classical array. 这些标记目前既保存在Collection - Markers中,又保存到更经典的数组中。 What I would like to now is to add new marker to the map as they are added by other users. 我现在想要的是在地图中添加新标记,因为它们是由其他用户添加的。
My question is: Is there any way to get notified when the collection is modified (eg another user add a marker) ? 我的问题是: 在修改集合时是否有任何方法可以获得通知(例如,另一个用户添加标记)?
Maybe this is not the best way to do it. 也许这不是最好的方法。 Any other suggestion? 还有其他建议吗?
if (Meteor.isClient) {
Template.hello.rendered = function(){
markersArray = [];
var latlng = new google.maps.LatLng(46.123, 8.843);
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
map.setCenter(latlng);
});
}
var opts = {
zoom: 16,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map-canvas"), opts);
// add a click event handler to the map object
google.maps.event.addListener(map, "click", function(event)
{
placeMarker(event.latLng);
});
Session.set('map', true);
function placeMarker(location) {
var marker = new google.maps.Marker({
position: location,
map: map
});
Markers.insert({"marker": location, "date": new Date()});
markersArray.push(marker);
}
}
}
You have to use observer 你必须使用观察者
Template.hello.rendered = function(){
Markers.find({}).observe({
added: function (m) {
// Add marker
}
});
}
As per the previous answer, you should use an observe
structure, but you really need to avoid leaving the observer running forever: 根据前面的答案,你应该使用一个observe
结构,但你真的需要避免让观察者永远运行:
Template.hello.rendered = function() {
this.markerObserve = Markers.find({}).observe({
added: function(m) {
placeMarker(m.location) // obviously depends on the structure of Markers documents
}
});
};
Template.hello.destroyed = function() {
this.markerObserve.stop();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.