0

I have this map with 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> 

It creates a lot markers and when you click on a maker a infowindow appers. How can I do that when a infowindow open if there is another open close it.

I need help. I tryed with this, to the final of the script:

 <?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 }?> 

But It doesn´t work.

1

1 Answer 1

0

You have to store information about infowindow that was opened the last time (your solution didn't work because of the variable scope in 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; ?>; }); 

btw. I don't think that generating javascript every time is a proper approach

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.