Google Maps API v3(一次打开一个infowindow) [英] Google Maps API v3 (one infowindow open at a time)

查看:105
本文介绍了Google Maps API v3(一次打开一个infowindow)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有人知道如何修改此代码,以便Google地图在您打开另一个地图时关闭infowindows?

Does someone know how to modify this code so that google maps closes infowindows when you open another?

换句话说,我只需要一个infowindow随时打开。我查看了一下stackoverflow,但似乎无法在此代码中实现人的解决方案。

In other words, I want only one infowindow open at all times. I looked around on stackoverflow but couldn't seem to implement people's solutions in this code.

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();

};


推荐答案

需要一个InfoWindow InfoWindow的API文档

There is a google recommendation what to do if you only want one InfoWindow API documentaion for InfoWindow.

这是:

It is:


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

InfoWindows may be attached to either Marker objects (in which case their position is based on the marker's location) or on the map itself at a specified LatLng. If you only want one info window to display at a time (as is the behavior on Google Maps), you need only create one info window, which you can reassign to different locations or markers upon map events (such as user clicks). Unlike behavior in V2 of the Google Maps API, however, a map may now display multiple InfoWindow objects if you so choose.

要更改信息窗口的位置,您可以通过在信息窗口中调用setPosition()来明确地更改其
位置,也可以通过
使用InfoWindow.open()方法将其附加到新标记。请注意
,如果您在不传递标记的情况下调用open(),InfoWindow将
使用InfoWindow
选项对象在构建时指定的位置。

To change the info window's location you may either change its position explicitly by calling setPosition() on the info window, or by attaching it to a new marker using the InfoWindow.open() method. Note that if you call open() without passing a marker, the InfoWindow will use the position specified upon construction through the InfoWindow options object.

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

So try to follow these suggenstions.

这篇关于Google Maps API v3(一次打开一个infowindow)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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