Google Maps API v3(一次打开一个infowindow)

时间:2013-02-27 11:59:22

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

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

换句话说,我希望任何时候都只打开一个信息窗口。我查看了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();

};

2 个答案:

答案 0 :(得分:2)

如果您只想要一个InfoWindow API documentaion for InfoWindow,Google会建议您做什么。

是:

  

InfoWindows可以附加到Marker对象(在这种情况下)   他们的位置是基于标记的位置)或地图本身   在指定的LatLng。如果您只想显示一个信息窗口   一段时间(就像Google地图上的行为一样),您只需创建一个   信息窗口,您可以将其重新分配到不同的位置或标记   在地图上发生事件(例如用户点击)。与V2中的行为不同   但是,谷歌地图API,地图现在可能会显示多个InfoWindow   如果您这样选择对象。

     

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

因此,请尝试遵循这些原因。

答案 1 :(得分: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().remove();
    }
    infowindow.open(map,marker);

});
相关问题