Javascript 如何通过数组获取多个值并在谷歌地图中显示此位置?
我正在从事一个项目,在基于NodeJS的Meteor中,我需要通过googlemapsapi在地图上创建“标记”(地点)。 这些标记位于“列表”数组中 这些各自的位置必须显示在地图上,每个位置都有各自的标记Javascript 如何通过数组获取多个值并在谷歌地图中显示此位置?,javascript,arrays,google-maps,meteor,google-maps-markers,Javascript,Arrays,Google Maps,Meteor,Google Maps Markers,我正在从事一个项目,在基于NodeJS的Meteor中,我需要通过googlemapsapi在地图上创建“标记”(地点)。 这些标记位于“列表”数组中 这些各自的位置必须显示在地图上,每个位置都有各自的标记 var count = 0; list.forEach(function(offer) { console.log(offer); // Animation Timeout setTimeout(function() { // Create the string into
var count = 0;
list.forEach(function(offer) {
console.log(offer);
// Animation Timeout
setTimeout(function() {
// Create the string into the infowindow
var contentString = '<p><strong>' + offer.place + '</strong></p>' +
'<p>' + offer.offer + '</p>';
// Create the infowindow object
var infowindow = new google.maps.InfoWindow({
content: contentString
});
// Add the marker on the map
var marker = new google.maps.Marker({
position: {lat: offer.loc.coordinates[1], lng: offer.loc.coordinates[0]},
map: map.instance,
animation: google.maps.Animation.DROP
});
// Add the infowindow for each marker
marker.addListener('click', function() {
infowindow.setContent(contentString);
infowindow.open(map, marker);
});
}, count * 200);
count++;
});
var计数=0;
列表。forEach(功能(报价){
控制台日志(报价);
//动画超时
setTimeout(函数(){
//在信息窗口中创建字符串
var contentString=''+offer.place+''+
“”+offer.offer+””;
//创建infowindow对象
var infowindow=new google.maps.infowindow({
内容:contentString
});
//在地图上添加标记
var marker=new google.maps.marker({
位置:{lat:offer.loc.coordinates[1],lng:offer.loc.coordinates[0]},
map:map.instance,
动画:google.maps.animation.DROP
});
//为每个标记添加信息窗口
marker.addListener('click',function()){
setContent(contentString);
信息窗口。打开(地图、标记);
});
},计数*200);
计数++;
});
然而,刚刚显示的只是第一个数组位置(“Teste2”),而不是所有的多个值通过“list”。可能是因为您在使用
infowindow
变量时没有任何声明
marker.addListener('click', function() {
infowindow.setContent(contentString);
infowindow.open(map, marker);
});
在设置标记侦听器之前,在循环中创建一个infowindow对象,如下所示:
let infowindow = new google.maps.InfoWindow({
content: contentString
});
编辑
也要改变那些台词
list.forEach(function(offer, index) { // <--- change for index
console.log(offer);
// [...]
// Add the infowindow for each marker
marker.addListener('click', function() {
infowindow.setContent(contentString); // <--- remove this one
infowindow.open(map, marker); // <--- map or map.instance ??
});
// Animation Timeout
setTimeout(function() {
// [...]
}, index * 200); // <--- change for index
});
list.forEach(函数(提供、索引){//你的代码是否抛出错误?在你的标记代码中,你正在使用map。实例
,而在你的信息窗口代码中,你正在使用map
。不,我的代码没有抛出错误。它正在工作……但是应用程序只显示数组“list”的第一个位置,我需要它显示“list”中的所有位置。从一个工作示例开始我创建了infowindow对象,但它还没有显示“列表”的所有位置。我很确定问题在于映射
/映射。实例
混乱。我更改了代码,就像你说的。我删除了所有计数
引用并添加了index
我用map
和map.instance
测试了这个应用程序,但是它不起作用!回答你的问题,没有新的google.maps.map()
,地图是用这个操作创建的GoogleMaps.ready('exampleMap',function(map){[…]map:map.instance,})
我忘了你用的是meteor,我的坏…所以我确认你必须同时使用map.instance
。我发现了这个,也许它会对你有帮助?如果没有,我有很多测试用例给你:1.你能尝试不使用setTimeout()
函数的循环吗?2.你能尝试在没有任何信息窗口代码的情况下添加标记吗(查看是否有错误停止循环)?我用你的代码创建了一个小提琴,它运行良好(没有meteor)。这就是为什么我没有对其进行任何更改。祝你好运!
list.forEach(function(offer, index) { // <--- change for index
console.log(offer);
// [...]
// Add the infowindow for each marker
marker.addListener('click', function() {
infowindow.setContent(contentString); // <--- remove this one
infowindow.open(map, marker); // <--- map or map.instance ??
});
// Animation Timeout
setTimeout(function() {
// [...]
}, index * 200); // <--- change for index
});