数据未正确循环通过Google地图信息窗口

时间:2013-02-19 16:58:11

标签: jquery json google-maps google-maps-api-3

我有一个通过JSON拉入的数据。它正确地拉入并将标记定位在它们的位置。 我遇到的问题是与google.maps.event.addListener有关,所有标记都在信息窗口中显示相同的内容,由于某些原因它似乎没有正确地循环数据。

以下是我所拥有的,关于为什么这种方法无法正常工作的任何想法,谢谢。

$.get('http://localhost/map/map/locations', function(result) {
    var locations = $.parseJSON(result);


    // Markers
    var markers = [];

    // Looping through the JSON data
    for (var i = 0, length = locations.length; i < length; i++) {

        var data = locations[i];

        // Creating a marker and putting it on the map
        var marker = new google.maps.Marker({
            position: new google.maps.LatLng(data.lat, data.long),
            map: map,
            title: data.title,
            icon: iconSrc[data.category]
        });
        markers.push(marker);


        google.maps.event.addListener(marker, 'click', (function(marker, i) {
            return function() {
                infoWindow.setContent("<div class='cont_box' id='" + data.entry_id + "'> <h2>" + data.title + "</h2> <div class='cont'><p><img src='" + data.infoImage + "' alt='' width='100' height='66' style='float:right;'>" + data.windowText + "</p></div><div style='clear:both;'></div><div class='link'><a href='" + data.moreLink + "' target='_blank'>" + data.moreText + "</a></div></div>");
                infoWindow.open(map, marker);
            }
        })(marker, i));

    }// END for loop

});

更新

将数据var添加到上下文后,它可以正常工作。

我现在必须根据类别显示和隐藏标记。我认为这可能是一个类似的问题。使用data var包含这些隐藏和显示函数的最佳方法是什么?

功能:

    /** Shows all markers of a particular category, and ensures the checkbox is checked **/
    function show(category) {
        for (var i=0; i<locations.length; i++) {
            if (data.category == category) {
                markers[i].setVisible(true);
            }
        }
    }// END function show



    /** Hides all markers of a particular category, and ensures the checkbox is cleared **/
    function hide(category) {
        for (var i=0; i<locations.length; i++) {
            if (data.category == category) {
              markers[i].setVisible(false);
            }
        }
    }// END function hide

    /** Show or hide the categories initially **/
    show("financial_services");
    show("government");
    show("identity_access");
    show("machine_to_machine");
    show("telecommunications");
    show("transport");

    /** Action when checkbox is clicked **/
    $(".checkbox").click(function(){
        var cat = $(this).attr("value");

        // If checked
        if ( $(this).is(":checked") ){
            show(cat);
        }
        else {
            hide(cat);
        }
    });

这就是当前它与所有内容的关系,它与$ .get调用有关,但在for循环中有数据var:

    // JSON feed
$.get('http://localhost/map/map/locations', function(result) {
    var locations = $.parseJSON(result);


    // Markers
    var markers = [];

    // Looping through the JSON data
    for (var i = 0, length = locations.length; i < length; i++) {

        var data = locations[i];

        // Creating a marker and putting it on the map
        var marker = new google.maps.Marker({
            position: new google.maps.LatLng(data.lat, data.long),
            map: map,
            title: data.title,
            icon: iconSrc[data.category]
        });
        markers.push(marker);


        google.maps.event.addListener(marker, 'click', (function(marker, i, loc) {
            return function() {
                infoWindow.setContent("<div class='cont_box' id='" + loc.entry_id + "'> <h2>" + loc.title + "</h2> <div class='cont'><p><img src='" + loc.infoImage + "' alt='' width='100' height='66' style='float:right;'>" + loc.windowText + "</p></div><div style='clear:both;'></div><div class='link'><a href='" + loc.moreLink + "' target='_blank'>" + loc.moreText + "</a></div></div>");
                infoWindow.open(map, marker);
            }
        })(marker, i, data));

    }// END for loop


    /** Shows all markers of a particular category, and ensures the checkbox is checked **/
    function show(category) {
        for (var i=0; i<locations.length; i++) {
            if (data.category == category) {
                markers[i].setVisible(true);
            }
        }
    }// END function show



    /** Hides all markers of a particular category, and ensures the checkbox is cleared **/
    function hide(category) {
        for (var i=0; i<locations.length; i++) {
            if (data.category == category) {
              markers[i].setVisible(false);
            }
        }
    }// END function hide

    /** Show or hide the categories initially **/
    show("financial_services");
    show("government");
    show("identity_access");
    show("machine_to_machine");
    show("telecommunications");
    show("transport");

    /** Action when checkbox is clicked **/
    $(".checkbox").click(function(){
        var cat = $(this).attr("value");

        // If checked
        if ( $(this).is(":checked") ){
            show(cat);
        }
        else {
            hide(cat);
        }
    });
});

1 个答案:

答案 0 :(得分:0)

您的addListener打开一个新的上下文,因此您的数据变量不可用。在上下文中添加数据var:

google.maps.event.addListener(marker, 'click', (function(marker, i, myData) {
    return function() {
       infoWindow.setContent("<div class='cont_box' id='" + myData.entry_id + "'> <h2>");
       infoWindow.open(map, marker);
    }
})(marker, i, data));