当另一个打开时关闭infowindow

时间:2013-07-04 14:00:01

标签: php javascript google-maps google-maps-api-3

我有Google Maps API v3的这张地图:

<?php
include('db_login.php');

      $connection = mysql_connect($db_host, $db_username, $db_password);
      if (!$connection){
      die ("Eror connecting database: <br/>". mysql_error());
      } 

      $db_select = mysql_select_db($db_database);
      if (!$db_select){
      die ("Database Error: <br/>". mysql_error());
      }

$query = mysql_query("SELECT * FROM routes"); 

?> 
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> 
<style type="text/css"> 
  html { height: 100% } 
  body { height: 100%; margin: 0px; padding: 0px } 
  #map_canvas { height: 100% } 
</style> 
<script type="text/javascript" 
    src="http://maps.google.com/maps/api/js?sensor=false&amp;language=es"> 
</script> 
<script type="text/javascript"> 


window.onload = function () {
  var options = {
    zoom: 5,
    center: new google.maps.LatLng(40.84706, -2.944336),
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };

  map = new google.maps.Map(document.getElementById('map_canvas'), options);


  <?php 
        for($i = 0; $i < mysql_num_rows($query); $i++){
            $icao = mysql_result($query, $i, 'from');
            $query2 = mysql_query("SELECT * FROM airports WHERE icao='$icao'");
            $latitude = mysql_result($query2, 0, 'latitude');
            $longitude = mysql_result($query2, 0, 'longitude'); 
            $city = mysql_result($query2, 0, 'city');
        ?> 
  var Airport1 = '<h3 align="center" style="font-family:Arial, Helvetica, sans-serif"><?php echo $icao; ?> - <?php echo $city; ?></h3>';
  var image = 'http://i46.tinypic.com/33zbm09.png';
  var latLonCenter = new google.maps.LatLng(<?php echo $latitude; ?>, <?php echo $longitude; ?>);
  marker<?php echo $icao; ?> = new google.maps.Marker({
    position: latLonCenter,
    map: map,
    draggable: false,
    icon: image,
    title: '<?php echo $icao; ?> - <?php echo $city; ?>',
    Airport1: Airport1
  });

    var infowindow<?php echo $icao; ?> = new google.maps.InfoWindow({
    content: Airport1
    });

    <?php
    $query4 = mysql_query("SELECT * FROM routes WHERE `from`='$icao'");


    for($y = 0; $y < mysql_num_rows($query4); $y++){
    $destination = mysql_result($query4, $y, 'to');
    $query5 = mysql_query("SELECT * FROM airports WHERE icao='$destination'");
    $latitude_destination = mysql_result($query5, 0, 'latitude');
    $longitude_destination = mysql_result($query5, 0, 'longitude'); 
    ?>

    var PolyLine<?php echo $destination;?> = new google.maps.Polyline({
        strokeColor: "#FF0000",
        strokeOpacity: 2.0,
        strokeWeight: 2
    });

    var polyCords<?php echo $destination;?> = [
    new google.maps.LatLng(<?php echo $latitude; ?>, <?php echo $longitude; ?>),
    new google.maps.LatLng(<?php echo $latitude_destination; ?>, <?php echo $longitude_destination; ?>)
    ];


    google.maps.event.addListener(marker<?php echo $icao; ?>, 'click', function() {
    <?php for($z = 0; $z < mysql_num_rows($query); $z++){
    $destination_delete = mysql_result($query, $z, 'to');?>

    PolyLine<?php echo $destination_delete;?>.setMap(null);

    <?php } ?>

    PolyLine<?php echo $destination;?>.setPath(polyCords<?php echo $destination;?>);

    PolyLine<?php echo $destination;?>.setMap(map);
    });

    google.maps.event.addListener(marker<?php echo $icao; ?>, 'click', function () {
      var n = 1;
      var infowindow<?php echo $icao; ?> = new google.maps.InfoWindow({
        content: "",
        maxWidth: 320,  
        zIndex: n 
      });

      infowindow<?php echo $icao; ?>.setContent(this.Airport1);
      infowindow<?php echo $icao; ?>.setZIndex(n++); 
      infowindow<?php echo $icao; ?>.open(map, marker<?php echo $icao; ?>);
    });

<?php } } ?>



}
</script> 
</head>  
<body> 
<center><div id="map_canvas" style="width:850px; height:560px;"></div></center> 
</body>

它会创建很多标记,当您点击制造商时,infowindow会出现。如果有另一个打开的情况,如果信息窗打开,我怎么能这样做呢。

我需要帮助。我尝试了这个,到剧本的最后:

    <?php for($j = 0; $j < mysql_num_rows($query); $j++){
    $icao_delete = mysql_result($query, $z, 'from');?>

    google.maps.event.addListener(map, "click", function(){ 
        infowindow<?php echo $icao_delete;?>.close(); 
    }); 

<?php }?>

但它不起作用。

1 个答案:

答案 0 :(得分:0)

您必须存储最后一次打开的infowindow信息(由于JS中的变量范围,您的解决方案无效):

var lastOpen = null;
google.maps.event.addListener(marker<?php echo $icao; ?>, 'click', function () {
  if (lastOpen!=null)
    lastOpen.close();
  var n = 1;
  var infowindow<?php echo $icao; ?> = new google.maps.InfoWindow({
    content: "",
    maxWidth: 320,  
    zIndex: n 
  });

  infowindow<?php echo $icao; ?>.setContent(this.Airport1);
  infowindow<?php echo $icao; ?>.setZIndex(n++); 
  infowindow<?php echo $icao; ?>.open(map, marker<?php echo $icao; ?>);
  lastOpen = infowindow<?php echo $icao; ?>;
});

顺便说一句。我不认为每次生成javascript都是正确的方法