循环数组映射

时间:2012-05-12 21:54:37

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

我需要帮助循环遍历数组并完成此代码。我的数组是从XML文件创建的。 XML文件包含要在地图上绘制的会议位置数据。阵列最终看起来像这样:

markerfilter[0].name
markerfilter[0].address
markerfilter[0].Meeting_Type
etc...
markerfilter[1].name
markerfilter[1].address
markerfilter[1].Meeting_Type
etc...

现在所有标记都在地图上绘制。我需要遍历数组并根据我设置的一些变量对其进行过滤。这是我尝试循环遍历数组:

for (var i = 0; i < markerfilter.length; i++) {
    if (markerfilter[i].Meeting_Type==type&&markerfilter[i].Day_of_Meeting==day&&markerfilter[i].Time_of_Meeting==time){
        //need to plot the points on the map here
        }else{
        //need to show blank map
        }
    }

这是我的整个代码。我甚至不知道循环是否在正确的位置或者代码的其他部分是否需要进入循环内部。任何帮助?

$(document).ready(function() {
    $("#map").css({
    height: 500,
    width: 600
});
var myLatLng = new google.maps.LatLng(43.653823, -79.382843);
MYMAP.init('#map', myLatLng, 11);

$("#showmarkers").click(function(e){
    MYMAP.placeMarkers('include/xml.php');
});
});

var MYMAP = {
map: null,
bounds: null
}

MYMAP.init = function(selector, latLng, zoom) {
var myOptions = {
    zoom:zoom,
    center: latLng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
}
this.map = new google.maps.Map($(selector)[0], myOptions);
this.bounds = new google.maps.LatLngBounds();
}

var markerfilter = [];
MYMAP.placeMarkers = function(filename) {
$.get(filename, function(xml){
    $(xml).find("marker").each(function(){
        var name = $(this).find('name').text();
        var address = $(this).find('address').text();
        var address2 = $(this).find('address2').text();
        var Meeting_Type = $(this).find('Meeting_Type').text();
        var Time_of_Meeting = $(this).find('Time_of_Meeting').text();
        var Day_of_Meeting = $(this).find('Day_of_Meeting').text();
        var Open_Meeting = $(this).find('Open_Meeting').text();
        var Wheelchair = $(this).find('Wheelchair').text();
        var ASL = $(this).find('ASL').text();
        var Comments = $(this).find('Comments').text();
        // create a new LatLng point for the marker
        var lat = $(this).find('lat').text();
        var lng = $(this).find('lng').text();
        var point = new google.maps.LatLng(parseFloat(lat),parseFloat(lng));

        var markerdata = {};
        $(this).children().each(function() {
            markerdata[this.tagName] = $(this).text();
        });
        markerfilter.push(markerdata);

        var MeetingType = document.getElementById("Meeting_Type");
        var type = MeetingType.options[MeetingType.selectedIndex].text;
        var DayofMeeting = document.getElementById("Day_of_Meeting");
        var day = DayofMeeting.options[DayofMeeting.selectedIndex].text;
        var TimeofMeeting = document.getElementById("Time_of_Meeting");
        var time = TimeofMeeting.options[TimeofMeeting.selectedIndex].text;

        for (var i = 0; i < markerfilter.length; i++) {
        if (markerfilter[i].Meeting_Type==type&&markerfilter[i].Day_of_Meeting==day&&markerfilter[i].Time_of_Meeting==time){
            markerfilter[i].setMap(MYMAP.map);
            }else{
            markerfilter[i].setMap(null);
            }
        }

        // extend the bounds to include the new point
        MYMAP.bounds.extend(point);

        var marker = new google.maps.Marker({
            position: point,
            map: MYMAP.map
        });

        var infoWindow = new google.maps.InfoWindow();
        var html='<b><u>'+name+'</b></u><br />'+address2+'<br />'+address+'<br />'+Meeting_Type+',&nbsp'+Time_of_Meeting+',&nbsp'+Day_of_Meeting+'<br />Open Meeting:&nbsp'+Open_Meeting+'<br />Wheelchair Accessible:&nbsp'+Wheelchair+'<br />ASL:&nbsp'+ASL+'<br />Comments:&nbsp'+Comments;
        google.maps.event.addListener(marker, 'click', function() {
            infoWindow.setContent(html);
            infoWindow.open(MYMAP.map, marker);
        });
        MYMAP.map.fitBounds(MYMAP.bounds);
    });
});
console.log(markerfilter);
}

现在,当我运行我的电流时,萤火虫告诉我: markerfilter [i] .setMap(null)不是函数。

1 个答案:

答案 0 :(得分:0)

关于错误: markerfilter [i] .setMap(null)不是函数,我想我看到了你的问题。在MYMAP.placemarkers函数中,您创建markerdata对象,定义某种赋予文本值的tagName属性,然后将markerdata推送到:{{1}数组:

markerfilter

然后大约10行,你有一个var markerdata = {}; $(this).children().each(function() { markerdata[this.tagName] = $(this).text(); }); markerfilter.push(markerdata); 循环试图检索代码看起来期望的是for数组中的标记:

markerfilter

由于for (var i = 0; i < markerfilter.length; i++) { if ( markerfilter[i].Meeting_Type==type && markerfilter[i].Day_of_Meeting==day && markerfilter[i].Time_of_Meeting==time ) { markerfilter[i].setMap( MYMAP.map ); } else { markerfilter[i].setMap( null ); } } 数组已加载markerfilter,因此它不包含标记,因此markerdata不是函数是有道理的,因为markerfilter[i].setMap(null)不是不是标记。希望这会有所帮助 -