mysql - 如何在谷歌地图标记上显示信息框

标签 mysql sql google-maps google-maps-api-3 mysqli

我有从数据库中获取数据并根据它显示标记的代码,但是我遇到了信息框的问题。他们没有得到正确显示。
例如,有 2 个标记。如果我单击第一个标记,则信息框会正确显示,然后在关闭它后,如果我单击另一个标记,则第一个标记的信息框会显示在第一个标记上,而第二个标记上不会显示任何内容。如果有人可以帮助我,将不胜感激

<?php 
    require 'connection.php'; 
    $parent_id = $_GET['country'];
    $fid = $_GET['fid']; 
        $sql = "SELECT * FROM features_for_office WHERE parent_id='".$parent_id."' and fid='".$fid."' ";
        $result = mysqli_query($con, $sql);
        if (mysqli_num_rows($result) > 0) 
            {?>
                <script>
                    var myCenter=new google.maps.LatLng(51.508742,-0.120850);
                    function initialize()
                    {
                        var mapProp = {
                          center:myCenter,
                          zoom:2,
                          mapTypeId:google.maps.MapTypeId.ROADMAP
                          };

                        var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);
                        <?
                            while($row = mysqli_fetch_assoc($result)) 
                                {
                                    $officeid= $row["officeid"];
                                    $sql1 = "SELECT * FROM register_office WHERE id='".$officeid."' ";
                                    $result1 = mysqli_query($con, $sql1);
                                    if (mysqli_num_rows($result1) > 0) 
                                        {
                                            while($row1 = mysqli_fetch_assoc($result1)) 
                                                {
                                                    $officelat= $row1["lat"];
                                                    $officelon= $row1["lon"];
                                                    $officetitle= $row1["officetitle"];
                                                    ?>
                                                    var marker=new google.maps.Marker({
                                                    position:new google.maps.LatLng(<?php echo $officelat; ?>, <?php echo $officelon; ?>),
                                                    });
                                                    marker.setMap(map);
                                                    var infowindow = new google.maps.InfoWindow({
                                                    content:"<?php echo $officetitle;?>"
                                                    });
                                                    google.maps.event.addListener(marker, 'click', function() {
                                                    infowindow.open(map,marker);
                                                    });
                                              <?}
                                        }
                                }?>
                    }
                    google.maps.event.addDomListener(window, 'load', initialize);
                </script>
            <?}
        mysqli_close($con);     
?>

最佳答案

您正在重新定义 markerinfowindow $result 的每一行一遍又一遍.推 marker每次将其定义为数组时,然后将事件监听器添加到标记数组元素。

例如:

...
var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);

var infowindow = new google.maps.InfoWindow();
var index = 0;
var markerArray = [];

...

while($row1 = mysqli_fetch_assoc($result1)) 
{
   $officelat= $row1["lat"];
   $officelon= $row1["lon"];
   $officetitle= $row1["officetitle"];
?>
   var marker=new google.maps.Marker({
      position:new google.maps.LatLng(<?php echo $officelat; ?>, <?php echo $officelon; ?>)
   });

   marker.setMap(map);

   markerArray.push(marker);

   google.maps.event.addListener(markerArray[index], 'click', function() {
      infowindow.setContent("<?php echo $officetitle;?>");
      infowindow.open(map,marker[index]);
   });

   index++;
<?}
...

这只是一个基本的实现,可以引用this链接,它很好地描述了处理多个标记。

关于mysql - 如何在谷歌地图标记上显示信息框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27542460/

相关文章:

php - mysql 数据库插入正在将所有 ID 更改为 4294967295

sql - 如何将类型 bytea 转换为 double

mysql - 我总是在 MySql 查询 : Error 1054: Unknown column 'column name' in field list 中收到错误

javascript - GeoJSON、重叠指针、OverlappingMarkerSpiderfier

javascript - 在 Google map 上显示路径时出现问题

mysql - 从每日数据中选择该月的平均值(每日总和/天数)

php - 使用 foreach 在 MySQL 中存储数组项

python - 两个互斥的多对多关系

html - GMaps API v3 加载,但无法在页面上查看

mysql - 两个日期时间 MySQL 之间的小时和分钟差异