Question

Hi i have this code which displays a marker with an infowindow everytime that an option is selected. is there any possible way of adding a picture in the info window so lets say if the option is sunny the sun.jpg is displayed in the info window and so on for the rest 2 options.At the moment 'hi' is displayed to the info window.

function dropweather()
{

 var  themarker;
var contentString = 'hi';

var infowindow = new google.maps.InfoWindow({
  content: contentString
 });
var lat = prompt ('Latitude'); var lng = prompt('Longitude');
switch(document.getElementById('weatherkind').value)
  {
    case 'sunny':
      themarker =  new google.maps.Marker({
            position: new google.maps.LatLng(lat,lng),
            map:map

        });
        break;
     case 'rainy':
       themarker= new google.maps.Marker({
            position: new google.maps.LatLng(lat,lng),
            map:map

        });
        break;
         case 'cloudy':
     themarker=   new google.maps.Marker({
            position: new google.maps.LatLng(lat,lng),
            map:map



        });
        break;

  }
google.maps.event.addListener(themarker, 'click', function() {
infowindow.open(map,themarker);
infowindow.setContent(this.html);
 });
 }

      google.maps.event.addDomListener(window,'load',initialize);
Was it helpful?

Solution

i can't find a reference to marker anywhere in your code.

so try

google.maps.event.addListener(marker, 'click', function() {
 infowindow.setContent(html);
 infowindow.open(map, this); //this refers to the marker that is clicked
});

if you have a variabe var marker

which refers to a valid marker like

var marker = new google.maps.Marker({
        position: new google.maps.LatLng(lat,lng),
        map:map

    });

then your code will work, if there is no scope issues. it's not needed since this will work fine

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top