Reputation: 49
I have a function that pulls data out of an XML file. I am trying to store that data in an array so I can loop through the array and filter it. So when "Time_of_Day" (From a dropdown menu) = "Time_of_Day" value in the array, my array will filter.
Right now I just need help building the array. Then I will try to understand how to loop through it and find the value for "Time_of_Day".
Here is my function to pull out data from the XML file. Right now my array is only storing the name values (mainly because I dont know what I am doing).
var markerfilter = new Array();
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();
markerfilter.push(name);
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;
// 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));
// 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+', '+Time_of_Meeting+', '+Day_of_Meeting+'<br />Open Meeting: '+Open_Meeting+'<br />Wheelchair Accessible: '+Wheelchair+'<br />ASL: '+ASL+'<br />Comments: '+Comments;
google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent(html);
infoWindow.open(MYMAP.map, marker);
});
MYMAP.map.fitBounds(MYMAP.bounds);
});
});
}
THe XML file that is created looks like this:
<markers>
<marker>
<name>AA at Greek Food</name>
<address>292 Danforth Avenue Toronto, ON</address>
<address2>Asteria</address2>
<Meeting_Type>AA</Meeting_Type>
<Time_of_Meeting>Late</Time_of_Meeting>
<Day_of_Meeting>Wednesday</Day_of_Meeting>
<Open_Meeting>Yes</Open_Meeting>
<Wheelchair>Yes</Wheelchair>
<ASL>Yes</ASL>
<Comments>Greek food is good.</Comments>
<lat>43.677322</lat>
<lng>-79.353729</lng>
</marker>
<marker>
<name>CA over Sushi</name>
<address>18 Jane Street, Toronto, ON</address>
<address2>ASA Sushi</address2>
<Meeting_Type>CA</Meeting_Type>
<Time_of_Meeting>Early</Time_of_Meeting>
<Day_of_Meeting>Monday</Day_of_Meeting>
<Open_Meeting>No</Open_Meeting><Wheelchair>Yes</Wheelchair>
<ASL>No</ASL>
<Comments>CA eating Sushi.</Comments>
<lat>43.649773</lat>
<lng>-79.484772</lng>
</marker>
<markers>
Upvotes: 0
Views: 76
Reputation: 272056
You probably want to do something like this:
$(xml).find("marker").each(function() {
markerfilter.push({
name: $(this).find("name").text(),
address: $(this).find("address").text()
// and so on
});
});
console.log(markerfilter);
// the markerfilter array will look like:
//
// [
// {name: "name1", address: "address1", /*...*/},
// {name: "name2", address: "address2", /*...*/},
// .
// .
// .
// ]
Looking at your XML, I think it is much easier to create an associative array like this:
$(xml).find("marker").each(function() {
var markerdata = {};
$(this).children().each(function() {
markerdata[this.tagName] = $(this).text();
});
markerfilter.push(markerdata);
});
console.log(markerfilter);
Upvotes: 1