gpt4 book ai didi

javascript - 如何在 Meteor 中向 Google map 添加新标记?

转载 作者:行者123 更新时间:2023-12-02 17:30:03 25 4
gpt4 key购买 nike

当前,当我点击给定位置时,我会在谷歌地图上添加标记。目前,标记既保存到集合 - 标记 - 又保存到更经典的数组中。我现在想要的是在其他用户添加新标记时向 map 添加新标记。

我的问题是:当集合被修改(例如另一个用户添加标记)时,有什么方法可以收到通知吗?

也许这不是最好的方法。还有其他建议吗?

Rumors

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);
}

}
}

最佳答案

根据前面的答案,您应该使用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();
}

关于javascript - 如何在 Meteor 中向 Google map 添加新标记?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23148700/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com