【问题标题】:Google Maps API v3 (one infowindow open at a time)Google Maps API v3(一次打开一个信息窗口)
【发布时间】:2013-02-13 05:28:58
【问题描述】:

有人知道如何修改此代码,以便谷歌地图在您打开另一个信息窗口时关闭信息窗口吗?

换句话说,我只想始终打开一个信息窗口。我查看了stackoverflow,但似乎无法在这段代码中实现人们的解决方案。

function initMapsDealers(){

objectLocation = new google.maps.LatLng(25.64152637306577, 1.40625);

var myOptions = {
    scrollwheel: false,
    zoom: 2,
    center: objectLocation,
    mapTypeId: google.maps.MapTypeId.ROADMAP
}

var map = new google.maps.Map(document.getElementById("map-canvas-dealers"), myOptions);
var image1 = '/gfx/iconPloegerGreen.png';
var image2 = '/gfx/iconPloegerGreen.png';
var image3 = '/gfx/iconPloegerDealer.png';

/* Info windows */
<?

function replace_newline($string) {
  return (string)str_replace(array("\r", "\r\n", "\n"), '', $string);
}


$i = 0;
foreach($dealers as $dealer)
{
    $dealerLanden[$dealer['Land']][] = $dealer;

    if($dealer['lat'] != "" && $dealer['lon'] != "")
    {
        $i++;

        ?>
        objectLocation<?= $i; ?> = new google.maps.LatLng(<?= $dealer['lat']; ?>, <?= $dealer['lon']; ?>);

        var contentString<?= $i; ?> =
            '<div class="infoWindow">'+
            '<strong><?= str_replace("'","", $dealer['name']); ?></strong><br>'+
            '<?= replace_newline($dealer['content']); ?>'+
            '</div>';

        var infowindow<?= $i; ?> = new google.maps.InfoWindow({
            content: contentString<?= $i; ?>
        }); 

        var marker<?= $i; ?> = new google.maps.Marker({
            position: objectLocation<?= $i; ?>,
            title:"<?= $dealer['name']; ?>",
            map: map,
            icon: <?

            if($dealer['group'] == "Hoofdkantoor"){ ?>image1<? }
            elseif($dealer['group'] == "Oxbo"){ ?>image2<? }
            elseif($dealer['group'] == "Dealers"){ ?>image3<? } 
            else{ ?>image1<? }?>

        }); 

        google.maps.event.addListener(marker<?= $i; ?>, 'click', function() {
            infowindow<?= $i; ?>.open(map,marker<?= $i; ?>);
        });
        <?
    }
}

?>                      

resizeSection();

};

【问题讨论】:

标签: javascript google-maps google-maps-api-3 infowindow


【解决方案1】:

如果您只想要一个 InfoWindow API documentaion for InfoWindow,有一个 google 建议该怎么做。

它是:

InfoWindows 可以附加到任一标记对象(在这种情况下 他们的位置基于标记的位置)或地图本身 在指定的 LatLng。如果您只希望一个信息窗口显示在 一次(就像谷歌地图上的行为一样),您只需要创建一个 信息窗口,您可以将其重新分配到不同的位置或标记 根据地图事件(例如用户点击)。与 V2 中的行为不同 然而,Google Maps API,一张地图现在可能会显示多个 InfoWindow 对象,如果你愿意的话。

要更改信息窗口的位置,您可以更改其 通过在信息窗口上调用 setPosition() 或通过显式定位 使用 InfoWindow.open() 方法将其附加到新标记。笔记 如果您在没有传递标记的情况下调用 open(),则 InfoWindow 将 通过 InfoWindow 使用构造时指定的位置 选项对象。

因此请尝试遵循这些建议。

【讨论】:

【解决方案2】:

这是我一次只打开一个信息窗口的解决方案:

infowindow = new google.maps.InfoWindow({
    content: infocontent,
    maxWidth: 200
});
google.maps.event.addListener(marker, 'click', function() {

    if($('.gm-style-iw').length) {
         $('.gm-style-iw').parent().hide();
    }
    infowindow.open(map,marker);
    
});

【讨论】:

  • remove() 函数会导致空异常。我认为最好使用 $(".gm-style-iw").parent("div").hide();
猜你喜欢
  • 2016-02-16
  • 2011-03-31
  • 2012-06-03
  • 1970-01-01
  • 2012-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多