How to check of which user-defined type a current JSON element is during $.each()?
Posted
by
Bob
on Stack Overflow
See other posts from Stack Overflow
or by Bob
Published on 2012-06-08T16:27:57Z
Indexed on
2012/06/08
16:40 UTC
Read the original article
Hit count: 150
I have the following structure for a JSON file:
({condos:[{Address:'123 Fake St.', Lat:'56.645654', Lng:'23.534546'},{... another condo ...},{...}],houses:[{Address:'1 Main Ave.', Lat:'34.765766', Lng:'27.8786674'},{... another house ...}, {...}]})
So, I have a list of condos and houses in one big JSON array.
I want to plot them all on my map, but I want to give condos and houses different marker icons( blue marker for condos, green marker for houses ).
Problem I have is - figuring out how to distinguish between types of markers when I $.each()
through them. How would I use if
to check whether I'm working with a condo or a house at the moment?
var markers = null;
$('#map').gmap(mapOptions).bind('init', function(){
$.post('getMarkers.php', function(json){
markers = json;
$.each(markers, function(type, dataMembers) {
$.each(dataMembers, function(i, j){
//if house use house.png to create marker
$('#map').gmap('addMarker', { 'position': new google.maps.LatLng(parseFloat(Lat), parseFloat(Lng)), 'bounds':true, 'icon':'house.png' } );
//if condo use condo.png
$('#map').gmap('addMarker', { 'position': new google.maps.LatLng(parseFloat(Lat), parseFloat(Lng)), 'bounds':true, 'icon':'condo.png' } );
});
});
});
});
© Stack Overflow or respective owner