【问题标题】:Google maps marker infowindow谷歌地图标记信息窗口
【发布时间】:2011-08-21 03:44:59
【问题描述】:
<?php
        //connect to your database ** EDIT REQUIRED HERE **
        mysql_connect("localhost","root","root"); //(host, username, password)

        //specify database ** EDIT REQUIRED HERE **
        mysql_select_db("Airport") or die("Unable to select database");
        //select which database we're using
        $query="select * from LondonHeathrowAirportArrivals ORDER BY id DESC";  
        // query string stored in a variable
        $rt=mysql_query($query);
        // query executed 
        echo mysql_error(); 

        while($nt=mysql_fetch_array($rt)){
            $counter=$counter+1;
            $angle=round($nt['angle'], -1);
        ?>
        <script type="text/javascript">
        function initialize() {

          var myLatlng = new google.maps.LatLng(51.48, -0.52);
          var myOptions = {
            zoom: 8,
            center: myLatlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
          }
          map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
          var flightPlanCoordinates = [
            path1, path2, path3, path4, path5, path6
          ];

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

          flightPath.setMap(map);
           for (i=1;i<=1000;i++){
              var marker<?= $nt['Flightnumber'] ?> = new google.maps.Marker({
                  position:eval('myLatlng'+i),
                  map: map, 
                  title:eval('title'+i),
                  icon: eval('image'+i)
              }); 
              google.maps.event.addListener(marker<?= $nt['Flightnumber'] ?>, 'click', function() {
                 alert('<?= $nt[Flightnumber] ?>');
                });

          }
        }
                var myLatlng<?php echo $counter ?>= new google.maps.LatLng(<?php echo $nt['Length'] ?>,<?php echo $nt['Breadth'] ?>);
                var image<?php echo $counter ?> = new google.maps.MarkerImage('img/plane<?= $angle ?>.png', new google.maps.Size(35, 35), new google.maps.Point(0,0), new google.maps.Point(0, 12));
                var title<?php echo $counter ?> = "<?= $nt[Flightnumber] ?>";
                var path1= new google.maps.LatLng(51.51,-0.60);
                var path2= new google.maps.LatLng(51.50,-0.58);
                var path3= new google.maps.LatLng(51.49,-0.57);
                var path4= new google.maps.LatLng(51.48,-0.55);
                var path5= new google.maps.LatLng(51.48,-0.53);
                var path6= new google.maps.LatLng(51.48,-0.52);
            </script>
            <?php               
            }
        ?>
</head>

有人可以向我解释为什么当我点击不同的标记时,我仍然收到包含最后一个标记内容的警报???如果有人可以更改上面的代码以使警报对每个人来说都是唯一的,我将非常感激......我已经坚持了几个小时并且找不到解决方案。

【问题讨论】:

  • 因为它是 JavaScript,你能给我们 URL 以便我们可以看到代码吗?
  • 对不起,这是本地的东西 :(...我可以给你完整的 JS 代码,我会编辑帖子:)

标签: javascript php google-maps google-maps-markers


【解决方案1】:

你遇到的是一个闭包问题,This post 有一个问题的简要定义和解决方案

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-20
    相关资源
    最近更新 更多