简体   繁体   English

如何使用API​​ V3在页面中显示多个Google Maps

[英]How to Display Multiple Google Maps in the page with API V3

I'm using google map API v3, and i have an issue. 我正在使用Google Map API v3,但是我遇到了问题。

I get my coordinates from data base and i need to display them with more than one map, and i don't know the number of maps. 我从数据库中获取坐标,因此需要用一张以上的地图显示它们,而我不知道地图的数量。

Here the code php code: 这里的代码是php代码:

<div id="view-content">
            <?php foreach ($this->tag as $tag) : ?>

                <div id="list">
                    <div id="left">
                        <div id="map_canvas" style=" margin-left: 5px; width: 136px; height: 136px;"></div>
                    </div>
                    <div id="right">
                        <div id="listtitre">
                            <a href="<?php echo $this->url(array('action' => 'maptag', 'controller' => 'maptag', 'module' => 'agent', 'idtag' => $tag['idtag'])); ?>"> 
                                <?php echo $tag['name']; ?>
                            </a>
                        </div>
                        <div id="tagdisctitle">
                            <font>Address: </font><br/>
                            <font>Latitude: </font><br/>
                            <font>Longitude: </font><br/>
                        </div>
                        <div id="taglistdisc">
                            <?php
                            if (strlen($tag["address"]) > 52)
                                echo substr($tag["address"], 0, 52) . "...";
                            else
                                echo substr($tag["address"], 0, 52) . ".";
                            ?> <br/>
                            <?php echo $tag['latitude']; ?><br/>
                            <?php echo $tag['longitude']; ?><br/>
                        </div>
                    </div> 
                </div>
            <?php endforeach; ?>
        </div>

The JS code: JS代码:

function initialize() {
    var myOptions = {
        center: new google.maps.LatLng(36.6464862, 9.9565553 ),
        zoom: 7,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("map_canvas"),
        myOptions);
    google.maps.event.addListener(map, 'click', function(event) {
        placeMarker(event.latLng);
    });

    var marker;
    function placeMarker(location) {
        if(marker){ //on vérifie si le marqueur existe
            marker.setPosition(location); //on change sa position
        }else{
            marker = new google.maps.Marker({ //on créé le marqueur
                position: location, 
                map: map,
                icon : "/images/mapgo.png"
            });
        }
        latitude.value=location.lat();
        longitude.value=location.lng();
    }

}

Thanks for any help 谢谢你的帮助

IDs must be unique on the page. ID在页面上必须唯一。 Since you are outputting your HTML in a loop you are creating duplicate IDs, including the map_canvas div. 由于要循环输出HTML,因此要创建重复的ID,包括map_canvas div。 You need to modify both your HTML and javascript so that a unique ID is created on each iteration. 您需要同时修改HTML和javascript,以便在每次迭代时都创建一个唯一的ID。 The simplest way to do this would be to keep a running counter that you increment on each iteration, and then include this counter in the ID: 最简单的方法是保持一个运行的计数器,该计数器在每次迭代时递增,然后将此计数器包括在ID中:

<div id="map_canvas<?=$counter?>"> [...] </div>

you'll then need to change your JS to use this as well. 然后,您需要更改JS才能使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM