3

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

换句话说,我希望始终只打开一个信息窗口。我在 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();

};
4

2 回答 2

3

如果您只想要 InfoWindow 的一个 InfoWindow API 文档,那么有一个 google 建议该怎么做。

这是:

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

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

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

于 2013-02-27T12:13:12.830 回答
3

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

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);
    
});
于 2014-01-17T14:25:44.440 回答