简体   繁体   English

所有信息窗口具有相同的数据

[英]All infowindows have same data

im fairly new to the Google Maps V3 API and i'm running into an issue where all infowindows have the same data, the code im using iterates through an array of marker details and outputs the marker plus an infowindow for each marker. 我是Google Maps V3 API的新手,我遇到了一个问题,即所有信息窗口都具有相同的数据,使用代码im遍历了一系列标记详细信息,并为每个标记输出标记以及一个信息窗口。 This is my code: 这是我的代码:

    <script type="text/javascript">

(function () {
google.maps.Map.prototype.markers = new Array();
google.maps.Map.prototype.addMarker = function(marker) {
this.markers[this.markers.length] = marker;
};})();

function initialize() {

var latlng = new google.maps.LatLng(37.0691, -8.0312);

    var myOptions = {
    zoom: 12,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

    var house = 'images/house.png';

    var a = new Array();

    var t =  new Object();
    t.name = "Casa De Cova"
    t.lat =  37.0691
    t.lng =  -8.0312
    t.ico = house
    t.info = 'Casa De Cova Villa'
    a[0] = t;

    var t =  new Object();
    t.name = "Casa Jaquinta"
    t.lat =  37.0450
    t.lng =  -8.0442
    t.ico = house
    t.info = 'Jaquinta Villa'
    a[1] = t;

    var t =  new Object();
    t.name = "Faro Aiport"
    t.lat =  37.016187
    t.lng =  -7.970581
    t.ico = 'images/plane.png'
    t.info = 'The main Airport'
    a[2] = t;


    for (var i = 0; i < a.length; i++) 
    {
        var latlng = new google.maps.LatLng(a[i].lat, a[i].lng);
        map.addMarker(createMarker(a[i].name,latlng,a[i].ico,a[i].info));
    }
}

var counter = 1;

function createMarker(name, latlng, icon, info) 
{
    var marker = new google.maps.Marker({position: latlng, map: map, icon: icon, title: name});

    infowindow = new google.maps.InfoWindow({            
        content: info
    });

    google.maps.event.addListener(marker, 'click', function() {
        infowindow.open(map,marker);
    });

    document.getElementById(counter).onclick = function() {
        infowindow.open(map, marker);
    }

    counter++;
    return marker;
}
</script>

The issue is sort of fixed if a 'var' declaration is made before 'infowindow.open(map,marker);' 如果在“ infowindow.open(map,marker);”之前进行“ var”声明,则该问题已得到解决。 if this is done then i can see each window with its respective data but then when one infowindow is opened the previous one will not close. 如果完成了,那么我可以看到每个窗口及其各自的数据,但是当打开一个信息窗口时,前一个窗口将不会关闭。

the site is here: http://www.danhall.co.uk/algarve/map.php 该网站在这里: http : //www.danhall.co.uk/algarve/map.php

I'm a bit lost, any help much appreciated. 我有点迷茫,任何帮助不胜感激。

Definitely use the var declaration for the infowindow; 绝对在信息窗口中使用var声明; otherwise infowindow is global and you're overwriting it every time you call createMarker() . 否则, infowindow是全局的,并且每次调用createMarker()时都会覆盖它。

After that, you just need to close all open infowindows every time you show one. 之后,每次显示一个窗口时,您只需要关闭所有打开的信息窗口即可。 Here's how I've solved this before: 这是我之前解决此问题的方法:

...
var counter = 1,
    infowindows = [];

function closeInfoWindows()
{
    var i = infowindows.length;
    while(i--)
    {
        infowindows[i].close();
    }
}

function createMarker(name, latlng, icon, info) 
{
    var marker = new google.maps.Marker({
        position: latlng,
        map: map,
        icon: icon,
        title: name
    });

    var infowindow = new google.maps.InfoWindow({            
        content: info
    });

    infowindows.push(infowindow);

    google.maps.event.addListener(marker, 'click', function ()
    {
        infowindow.open(map,marker);
    });

    document.getElementById(counter).onclick = function ()
    {
        infowindow.open(map, marker);
    }

    counter++;
    return marker;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM