sanjay sanjay - 3 months ago 7
Javascript Question

How to write if condition in google map function? How to dynamically change image?

I want to change the image in google map based on the dynamic values

if

value === 1
, it should be
1.jpg
, if
value === 2
it should be
2.jpg
like that. I need to change the images based on the JSON data coming from data base. 3 images empty bin, normal bin , full bin

<head>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCLi6l" type="text/javascript"></script>


<script>

var myCenter = new google.maps.LatLng(22.8046, 86.2029);
function initialize()
{
var mapProp = {
center:myCenter,
zoom:14,
mapTypeId:google.maps.MapTypeId.ROADMAP
};

var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);
var image = 1;
var marker=new google.maps.Marker({
position:myCenter,
icon:'img/dustbin3.png'
});


marker.setMap(map);
}

google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>

<body>
<div id="googleMap" style="width:1580px;height:780px;"></div>

</body>

Answer

i'm not sure if it't the right thing wjat you're looking for but i can suggest you to try this one

var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);

var value = //result of your JSON data
var v_icon='';

if(value<50){
   v_icon = '1.jpg';
}else if(value>50 && value<90){
   v_icon = '2.jpg';
}else if(value>90){
   v_icon = '3.jpg';
}

var marker=new google.maps.Marker({
  position:myCenter,
  icon: v_icon
});

if it is not that so i didn't understood well what are you trying to achieve :)

Comments