Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
531 views
in Technique[技术] by (71.8m points)

javascript - How to keep single Info window open at the same time in Google map V3?

aI know that this one is repeated question,

I am using google map v3 on my Django web based applicaiton. Where i am using Markers, Infowindow and polyline. Everything works fine, except the one when i click on a marker to show the content by Info window, the previous opened info window didn't closed.

I am posting the my map code(only the script part or which are the useful):

var marker = add_marker(flightPlanCoordinates[i][0], flightPlanCoordinates[i][1],"Event Detail",myHtml);

Here myHtml is a variable which contains the Info window content. I didn't define the variable here. SO ignore it.

    marker.setMap(map);
    }

    var flightPath = new google.maps.Polyline({
                 path: flightPlanCoordinatesSet,
                 strokeColor: "#FF0000",
                 strokeOpacity: 1.0,
                 strokeWeight: 2
                  });
    flightPath.setMap(map);
}

function add_marker(lat,lng,title,box_html) {
var infowindow = new google.maps.InfoWindow({
    content: box_html
});

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

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

return marker;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Instead of multiple infoWindows use only one instance.

When clicking on a marker, first close the infoWindow, then set the new content and open the infoWindow.

function add_marker(lat,lng,title,box_html) 
{
  //create the global instance of infoWindow 
  if(!window.infowindow)
  {
    window.infowindow=new google.maps.InfoWindow();
  } 

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

  google.maps.event.addListener(marker, 'click', function() {
    infowindow.close();
    infowindow.setContent(box_html);
    infowindow.open(map,this)
  });   

  return marker;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...