多个Google地图在一个页面上的问题

时间:2016-02-08 11:36:53

标签: php mysql google-maps maps smarty

我在一页上显示多个Google地图时遇到问题

第一张地图显示正确但第二张不显示

我使用Smarty来显示模板。结果来自db。任何人都可以检查代码并帮助我显示多个地图

{foreach item=row  from=$adress}
                <tr>
                  <td>
                    <script type="text/javascript">
                      function initialize() {
                        var position = new google.maps.LatLng({$row->lat}, {$row->longi});
                        var myOptions = {
                          zoom: 12,
                          center: position,
                          mapTypeId: google.maps.MapTypeId.ROADMAP
                        };
                        var map = new google.maps.Map(
                            document.getElementById("map_canvas{$row->site_id}"),
                            myOptions);

                        var marker = new google.maps.Marker({
                            position: position,
                            map: map,
                            title:"This is the place."
                        });  

                        var contentString = 'Hello <strong>World</strong>!';
                        var infowindow = new google.maps.InfoWindow({
                            content: contentString
                        });

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

                      }

                    </script>
                  <div id="map_canvas{$row->site_id}" style="width:300px;height:200px;"></div>

1 个答案:

答案 0 :(得分:0)

这是你的HTML代码: -

</head>
<body onload="initialize()">
<H1 align="center">Two Google maps side-by-side</h1>
<div id="map_canvas1" style="top: 10px; left: 25px; width:210px; height:220px; float: left"></div>
<div id="map_canvas2" style="top: 10px; left: 75px; width:210px; height:220px"></div>
</body>
</html>

<script>


    function initialize()
    {
        <?php 

    for($i=1; $i<=2; $i++){

    ?>

        var latlng<?php echo $i ?> = new google.maps.LatLng(18.520266,73.856406);

        var myOptions =
        {
            zoom: 15,
            center: latlng<?php echo $i; ?>,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };


        var map<?php echo $i; ?> = new google.maps.Map(document.getElementById("map_canvas<?php echo $i ?>"), myOptions);



        var myMarker<?php echo $i; ?> = new google.maps.Marker(
        {
            position: latlng<?php echo $i; ?>,
            map: map<?php echo $i; ?>,
            title:"Pune"
       });

    <?php } ?>

    }


    </script>

我知道您使用不同的编程语言来定义代码。但我希望你明白这个逻辑。