Reputation: 175
I have written this code to show multiple locations on d map. But I want the marker to be red if the location[i].visit_status
is 0 and green if the location[i].visit_status
is 1 ( I have the green and red icons in my machine ) so I wrote this code but for a reason that I don't know, it is not working. can anyone help me please?
Here is the code:
var locations=[{"id_retailer":"3","retailer_name":"ret1","retailer_latitude":"3.083826","retailer_longitute":"101.731689","visit_date":"2011-11-11","visit_status":"0"}];
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 8,
center: new google.maps.LatLng(2.67968661580376,102.23876953125),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow();
var marker, i;
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i].retailer_latitude, locations[i].retailer_longitute),
map: map,
if(locations[i].visit_status)
icon: 'images/1.png';
else
icon: 'images/0.png';
title: locations[i].retailer_name
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent("Name: "+locations[i].retailer_name);
infowindow.open(map, marker);
}
})(marker, i));
}
window.onunload=GUnload;
Upvotes: 0
Views: 121
Reputation: 59485
I'd recommend the same as Adilson, moreover, don't forget to set the icon shadow - this is example of the standard icon shadow (if you using icons that have the same shape as the standard one):
icon: locations[i].visit_status ? 'images/1.png' : 'images/0.png',
shadow: new google.maps.MarkerImage(
'http://maps.gstatic.com/mapfiles/shadow50.png',
null,
null,
new google.maps.Point(10, 34)
)
I'd like to know some better way how to specify default shadow, but I don't know - see this question.
Upvotes: 0
Reputation: 2755
Change this syntax
map: map,
if(locations[i].visit_status)
icon: 'images/1.png';
else
icon: 'images/0.png';
by this other one:
map: map,
icon: locations[i].visit_status ? 'images/1.png':'images/0.png',
Upvotes: 1