0

实际上我正在从 Maps v2 迁移到 v3。但我面临一个非常奇怪的错误。这是我的代码

function createMarker(arrayPos,title,posn) {
    var size = {width:15,height:15};
    var iconSize = new google.maps.Size(size.width,size.height);
    var iconAnchor = new google.maps.Point(9, 34);
    var marker = new google.maps.Marker({
        icon: new google.maps.MarkerImage("my.png",iconSize,null,iconAnchor),
        title: title,
        animation: google.maps.Animation.DROP,
        position: posn
    });
    marker.html = getMarkerHtml(arrayPos);
    var infowindow = new google.maps.InfoWindow();  
    infowindow.setContent(marker.html);
    infowindow.open(map,marker);
    google.maps.event.addListener(marker, 'click', function() {
        infowindow.setContent(marker.html);
        infowindow.open(map,marker);
    });
    return marker;
}

在此,我正在尝试在添加标记后立即打开信息窗口以及点击侦听器。在此所有信息窗口都会打开,但侦听器始终未在第一个标记上注册。这有什么指示吗?

4

1 回答 1

1

我已将您的代码修改如下,并且可以正常工作。你会检查它是否解决了你的问题?

为了模拟标记单击,您应该添加:google.maps.event.trigger(marker, 'click');

function createMarker(arrayPos,title,posn) {
    var size = {width:15,height:15};
    var iconSize = new google.maps.Size(size.width,size.height);
    var iconAnchor = new google.maps.Point(9, 34);
    var marker = new google.maps.Marker({
        icon: new google.maps.MarkerImage("my.png",iconSize,null,iconAnchor),
        title: title,
        animation: google.maps.Animation.DROP,
        position: posn
    });
    //marker.html = getMarkerHtml(arrayPos);
    var infowindow = new google.maps.InfoWindow();  
    //infowindow.setContent(marker.html);
    //infowindow.open(map,marker);

    var markersHTML = getMarkerHtml(arrayPos);

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

    return marker;
}

例子:

<!doctype html>
<html lang="en">
   <head>
        <title>Google Maps</title>
        <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
        <script type="text/javascript" src="http://maps.google.com/maps/api/js?v=3&sensor=false&language=en"> </script>
        <script type="text/javascript">

            var cityList = [
                    ['Chicago', 41.850033, -87.6500523, 1],
                    ['Illinois', 40.797177,-89.406738, 2]
                ],
                demoCenter = new google.maps.LatLng(41,-87),
                map;

            function initialize()
            {
                map = new google.maps.Map(document.getElementById('map_canvas'), {
                   zoom: 7,
                   center: demoCenter,
                   mapTypeId: google.maps.MapTypeId.ROADMAP
                 });
            }

            function addMarkers()
            {
                var marker, 
                i,
                infowindow = new google.maps.InfoWindow();

                for (i = 0; i < cityList.length; i++) 
                {  
                    marker = new google.maps.Marker({
                        position: new google.maps.LatLng(cityList[i][1], cityList[i][2]),
                        map: map,
                        title: cityList[i][0]
                    });

                    var markersHTML = ["<div><a href=\"test\">",cityList[i][0],"</a></div>"].join("");

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

            $(document).ready(function() {
                initialize();
            });

            $(document).on('click', '.add-markers', function(e) {
                e.preventDefault();
                addMarkers();
            });

        </script>
    </head>
    <body>
        <div id="basic-map">
            <div id="map_canvas" style="height:350px;"></div>
            <a href="#" class="add-markers">Add Some Markers</a>
        </div>      
    </body>
</html>

我希望这有帮助。

于 2013-03-02T13:18:55.230 回答