Google地图标记加载最后一条记录

时间:2011-10-18 12:25:01

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

我正在尝试将一组标记添加到谷歌地图,每个标记都有一个信息窗口。我使用php和javascript混合来做这个,php从数据库中获取所有记录,然后javascript将所有标记添加到地图中,并使用从数据库中获取的坐标。

这是我到目前为止所做的:

<script type="text/javascript">
        function initialize() {

            var latlng = new google.maps.LatLng(<?=$lat?>, <?=$long?>);

            var settings = {
                zoom: 12,
                center: latlng,
                mapTypeControl: false,
                navigationControl: true,
                navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
                mapTypeId: google.maps.MapTypeId.ROADMAP};
            var map = new google.maps.Map(document.getElementById("map_canvas"), settings);

            var postcodeLogo = new google.maps.MarkerImage('/images/postcode_marker.png',
                new google.maps.Size(32,37),
                new google.maps.Point(0,0),
                new google.maps.Point(15,34)
            );

            var propertyLogo = new google.maps.MarkerImage('/images/property_marker.png',
                new google.maps.Size(32,37),
                new google.maps.Point(0,0),
                new google.maps.Point(15,34)
            );

            var infowindow = new google.maps.InfoWindow();  

            <?
            foreach ($results as $grid_refs)
            {
            ?>          

                    var propPos = new google.maps.LatLng(<?=$grid_refs['grid_ref']?>);

                    propMarker = new google.maps.Marker({
                        position: propPos,
                        map: map,
                        icon: propertyLogo,
                        title:"<?=$grid_refs['sd_name']?>",
                        zIndex: 3});

                    var contentString = '<div style="font-weight:bold;"><?=$grid_refs['sd_name']?></div><div><img src="www.imagelink.com/<?=$grid_refs['image_filename']?>"></div><div><a href="http://www.pagelink-<?=$grid_refs['property_id'];?>/" target="-blank">See full details</a></div>';

                    google.maps.event.addListener(propMarker, 'click', function() {
                            infowindow.setContent(contentString);
                            infowindow.open(map,this);
                        });

            <?
            }
            ?>


            var positionPos = new google.maps.LatLng(<?=$lat?>, <?=$long?>);

            var positionMarker = new google.maps.Marker({
                position: positionPos,
                map: map,
                icon: postcodeLogo,
                title:"<?=$_GET['searchpostcode']?>",
                zIndex: 3});


        }
    </script>

positionMarker是用户输入他们想要搜索的邮政编码的地方。 propMarker标记地图上随后显示的所有位置。

在代码中显示所有信息,但是在地图上,每个信息窗口都会打开最后一条检索到的记录 - 我希望它加载与该标记相关的信息。

我看过这个问题:

Google Maps infoWindow only loading last record on markers

哪个会起作用,但是我似乎无法使用php foreach获得一个循环的js,我已经在php foreach内外尝试了它。

我认为我需要的是forearch循环,以便在每次创建标记时更改变量和contentString的名称,但我似乎可以使其工作?

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:3)

您将循环所有标记,但在最后一次迭代中,您设置为contentString的内容将成为所有点击事件的内容。

相反,将其委托给另一个函数(或使用闭包),例如

            <?
            foreach ($results as $grid_refs)
            {
            ?>          

                    var propPos = new google.maps.LatLng(<?=$grid_refs['grid_ref']?>);

                    propMarker = new google.maps.Marker({
                        position: propPos,
                        map: map,
                        icon: propertyLogo,
                        title:"<?=$grid_refs['sd_name']?>",
                        zIndex: 3});

                    var contentString = '<div style="font-weight:bold;"><?=$grid_refs['sd_name']?></div><div><img src="www.imagelink.com/<?=$grid_refs['image_filename']?>"></div><div><a href="http://www.pagelink-<?=$grid_refs['property_id'];?>/" target="-blank">See full details</a></div>';

                    // add an event listener for this marker
                    bindInfoWindow(propMarker, map, infowindow, contentString);
            <?
            }
            ?>

然后添加一个新功能:

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