infowindow关闭和var(谷歌地图api v3) [英] infowindow closing and var (google maps api v3)

查看:147
本文介绍了infowindow关闭和var(谷歌地图api v3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在google maps api v.3中打开新的关闭infowindow时遇到了问题。

  var infowindow; 
函数addMarker(id,location){
contentString ='some content';

var marker = new google.maps.Marker({
position:location,
map:map
});

var infowindow = new google.maps.InfoWindow({
content:contentString,
maxWidth:200
});

google.maps.event.addListener(marker,'click',function(){
if(infowindow)infowindow.close();
infowindow.open(map,标记);
});

markersArray [id] = marker;





$ b

问题是,在上面的代码中,旧的infowindow在新的除非我从行中删除 var inf infindindow = new google.maps.InfoWindow({
),但所有infowindows都具有相同的内容...



任何帮助?
谢谢。

解决方案

一个全局变量包含一个指向上一个打开的标记的指针,当打开一个新的标记时,关闭前一个。像这样

  // var infowindow; //<  -  removed; should not be global 
var openedInfoWindow = null; //< - 在此添加;确保它是全局的

函数addMarker(id,location){
contentString ='some content';

var marker = new google.maps.Marker({
position:location,
map :map
});

var infowindow = new google.maps.InfoWindow({
content:contentString ,
maxWidth:200
});

google.maps.event.addListener(marker,'click',function(){
if(openedInfoWindow!= null)openedInfoWindow.close(); //< - changed这个
infowindow.open(map,marker);
//在接下来的4行添加
opensInfoWindow = infowindow;
google.maps.event.addListener(infowindow,'closeclick', function(){
openedInfoWindow = null;
});
});

markersArray [id] = marker;
}


I have problem with closing infowindow when new is open in google maps api v.3.

var infowindow;
function addMarker(id, location) {
contentString = 'some content';

var marker = new google.maps.Marker({
  position: location,
  map: map
});

var infowindow = new google.maps.InfoWindow({
    content: contentString,
    maxWidth: 200
});

google.maps.event.addListener(marker, 'click', function() {
  if (infowindow) infowindow.close();  
  infowindow.open(map, marker); 
});

markersArray[id] = marker;
}

The problem is that in above code the old infowindow is not close when new one is clicked unless I delete var from the line var infowindow = new google.maps.InfoWindow({ But then all the infowindows have the same content...

Any help? Thanks.

解决方案

Use a global variable to contain a pointer to the last opened marker, and when opening a new one, close the previous one. Like this

//var infowindow; // <-- removed; should not be global
var openedInfoWindow = null;  // <-- added this here; make sure it's global

function addMarker(id, location) {
    contentString = 'some content';

    var marker = new google.maps.Marker({
        position: location,
        map: map
    });

    var infowindow = new google.maps.InfoWindow({
        content: contentString,
        maxWidth: 200
    });

    google.maps.event.addListener(marker, 'click', function() {
      if (openedInfoWindow != null) openedInfoWindow.close();  // <-- changed this
      infowindow.open(map, marker); 
      // added next 4 lines
      openedInfoWindow = infowindow;
      google.maps.event.addListener(infowindow, 'closeclick', function() {
          openedInfoWindow = null;
      });
    });

    markersArray[id] = marker;
}

这篇关于infowindow关闭和var(谷歌地图api v3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆