谷歌地图信息窗口关闭?

发布于 2024-10-20 06:55:51 字数 1975 浏览 2 评论 0原文

作为我的last.fm/google 地图事件混搭的一部分,我必须将标记从last.fm API 动态绘制到google 地图上。

这一切都很好,但是当我单击标记时,只显示最后一个信息窗口(针对一场演出)。我知道这样做的原因,但很难实施。

目前,我正在通过所有动态演出的位置(坐标)运行 PHP 循环,然后将其传递给 JavaScript。这对我来说更有意义 - 而且我对 PHP 的了解比 JS 好得多:

<?php 

            foreach ($gigs->events->event as $js_event) { 

            $lat = $js_event->venue->location->children("geo",true)->point->children("geo",true)->lat;
            $long = $js_event->venue->location->children("geo",true)->point->children("geo",true)->long;
            $coords = "$lat,$long"; 

            ?>      

            var image = new google.maps.MarkerImage('http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=G|00CC99|000000',
            new google.maps.Size(40, 32),
            // The origin for this image is 0,0.
            new google.maps.Point(0,0),
            // The anchor for this image is the base of the flagpole at 0,32.
            new google.maps.Point(0, 32));

                    var marker = new google.maps.Marker({

                      position: new google.maps.LatLng(<?php echo $coords ?>),
                      map: map,
                      icon:image, 
                      title: '<?php echo str_replace('\'','',$js_event->title) ." at ". str_replace('\'','',$js_event->venue->name) ?>'

                    });

                    var contentString = '<?php echo str_replace('\'','',$js_event->title) ." at ". str_replace('\'','',$js_event->venue->name)?>'

                    var infowindow = new google.maps.InfoWindow({
                        content: contentString
                    });

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

                <? } ?>

如何在不将循环完全重构为 JS 而不是 PHP 的情况下添加闭包,等等。除非这是唯一的解决方案之一?

非常非常感谢。

as part of my last.fm/google maps event mashup, I have to plot markers dynamically from last.fm API onto the google map.

This is all well but when I click the marker, only the last infowindow (for one gig) is displayed. I know the reasoning for this but struggle to implement it.

Currently I'm running a PHP loop through all the dynamic gig's locations (co-ordinates) and then passing this to the javascript. This makes more sense to me - and my knowledge of PHP is much better than JS:

<?php 

            foreach ($gigs->events->event as $js_event) { 

            $lat = $js_event->venue->location->children("geo",true)->point->children("geo",true)->lat;
            $long = $js_event->venue->location->children("geo",true)->point->children("geo",true)->long;
            $coords = "$lat,$long"; 

            ?>      

            var image = new google.maps.MarkerImage('http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=G|00CC99|000000',
            new google.maps.Size(40, 32),
            // The origin for this image is 0,0.
            new google.maps.Point(0,0),
            // The anchor for this image is the base of the flagpole at 0,32.
            new google.maps.Point(0, 32));

                    var marker = new google.maps.Marker({

                      position: new google.maps.LatLng(<?php echo $coords ?>),
                      map: map,
                      icon:image, 
                      title: '<?php echo str_replace('\'','',$js_event->title) ." at ". str_replace('\'','',$js_event->venue->name) ?>'

                    });

                    var contentString = '<?php echo str_replace('\'','',$js_event->title) ." at ". str_replace('\'','',$js_event->venue->name)?>'

                    var infowindow = new google.maps.InfoWindow({
                        content: contentString
                    });

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

                <? } ?>

How could I add closure without fully refactoring the loops to JS and not PHP, etc. Unless this is one of the only solutions?

Many, many thanks.

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

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

发布评论

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

评论(1

你怎么敢 2024-10-27 06:55:51

隔离 marker 变量范围的一种简单方法是将调用包装在匿名函数中:

var map = ...

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

如所述此处,匿名函数将看到其声明范围内的任何内容。所以在函数内部可以看到map,它在声明时就在作用域内。但在函数外部,marker 是不可见的,因此匿名函数的重复克隆不会相互影响。

An easy way to isolate the scope of your marker variable is to wrap the invocation in an anonymous function:

var map = ...

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

As described here, the anonymous function will see anything in scope where it was declared. So inside the function can see map, which was in scope when it was declared. But outside the function, marker is invisible, so repeated clones of the anonymous function won't impact each other.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文