谷歌地图标记加载最后一条记录

发布于 2024-12-10 13:16:15 字数 3271 浏览 0 评论 0原文

我正在尝试向谷歌地图添加一组标记,每个标记都有一个信息窗口。我使用 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 地图 infoWindow 仅加载最后一条记录在标记上,

这将起作用,但我似乎无法获得与 php foreach 一起使用的 js for 循环,我已经在 php foreach 内部和外部尝试过它。

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

非常感谢任何帮助!

I am trying to add a group of markers to google maps each with a infomation window. I am using a mix of php and javascript to do this, the php gets all the records from the database and then the javascript adds all the markers to the map with the coordinates taken from the database.

Here is what I have so far:

<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>

The positionMarker is where the user enters the postcode they want to search by. The propMarker marks all of the places on the map which then show.

In the code all the information is showing, however on the map each info window opens with the last retrieved record - where I want it to load the information relevant to that marker.

I have looked at this question:

Google Maps infoWindow only loading last record on markers

which will work, but I cant seem to get a js for loop working with the php foreach, I have tried it within the php foreach and outside of.

I THINK what I need is the forearch loop to change the name of the variable and contentString each time it creates a marker, but I can seem to get this working?

Any help much appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲歌长辞 2024-12-17 13:16:15

发生的情况是,您循环所有标记,但在最后一次迭代时,您设置到 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);
    });
} 

What happens is that you loop round all the markers, but at the very last iteration, that content that you've set into contentString becomes what's then used for all the click events.

Instead, delegate this off to another function (or use a closure), e.g.

            <?
            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);
            <?
            }
            ?>

Then add a new function:

function bindInfoWindow(marker, map, infowindow, html) {
    google.maps.event.addListener(marker, 'click', function() {
        infowindow.setContent(html);
        infowindow.open(map, marker);
    });
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文