如何在谷歌地图api位置第二次点击时从侧边栏关闭infoWindow [英] How to close infoWindow from sidebar in the second click in google maps api places

查看:110
本文介绍了如何在谷歌地图api位置第二次点击时从侧边栏关闭infoWindow的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码: http:/ /gmaps-samples-v3.googlecode.com/svn/trunk/places/places-search.html



我想要第二次点击关闭infowindow(var iw)。

 函数isInfoWindowOpen(iw){
var map = iw .getMap();
return(map!== null&& typeof map!==undefined);
}

tr.onclick = function(){
google.maps.event.trigger(markers [i],'click');
console.log('markers [i]:'+ i);

if(isInfoWindowOpen(iw)){
//如果IW打开,做些事
iw.close(map,markers [i]);



$ div $解析方案

更改要检查的示例中的showInfoWindow函数看看infowindow是否是开放的,并且是否与infowindow相同,然后关闭它,否则执行所需的处理以获取内容。

 函数showInfoWindow(i)需要在标记中添加一个属性,以便判断它是否相同。 {
return function(place,status){
if(!! iw&& iw._iwId == i){
iw.close();
iw = null;
} else {
if(iw){
iw.close();
iw = null;
}
if(status == google.maps.places.PlacesServiceStatus.OK){
iw = new google.maps.InfoWindow({
content:getIWContent(place),
_iwId:i
});
iw.open(map,markers [i]);
}
}
}
}

< a href =http://www.geocodezip.com/v3_GoogleEx_places-search_closeOn2ndClick.html =nofollow>工作示例


I'm using this code: http://gmaps-samples-v3.googlecode.com/svn/trunk/places/places-search.html

I'd like on the second click of the sidebar, to close the infowindow (var iw).

    function isInfoWindowOpen(iw){
        var map = iw.getMap();
        return (map !== null && typeof map !== "undefined");
    }

tr.onclick=function(){
        google.maps.event.trigger(markers[i],'click');
        console.log('markers[i]: '+i);

    if (isInfoWindowOpen(iw)){
        // do something if IW is open
        iw.close(map,markers[i]);
    }
}

解决方案

Change the showInfoWindow function in the example to check to see if the infowindow is open and the same infowindow, then just close it, otherwise do the required processing to get the contents. Need to add a property to the marker so you can tell whether it is the same one or not.

  function showInfoWindow(i) {
    return function(place, status) {
      if (!!iw && iw._iwId == i) {
        iw.close();
        iw = null;
      } else {
        if (iw) {
          iw.close();
          iw = null;
        }
        if (status == google.maps.places.PlacesServiceStatus.OK) {
          iw = new google.maps.InfoWindow({
            content: getIWContent(place),
            _iwId:i
          });
          iw.open(map, markers[i]);        
        }
      }
    }
  }

working example

这篇关于如何在谷歌地图api位置第二次点击时从侧边栏关闭infoWindow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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