Google Map Infowindow关闭? [英] Google Map Infowindow Closure?

查看:249
本文介绍了Google Map Infowindow关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



这些都是好吧,但是当我点击标记时,只显示最后一个infowindow(对于一个演出)。我知道这个理由,但是很难实现它。

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

 <?php 

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

$ lat = $ js_event-> venue-> location-> children(geo ,真) - >点对多点>儿童( 地理,真) - > 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),
//此图片的来源是0,0。
new google.maps.Point(0,0),
//这个图片的锚点是旗杆底部的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->场地 - >名称)?>'

});

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的情况下添加闭包,除非这是唯一的解决方案之一是什么?



非常感谢。

解决方案

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

 var map = ... 

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

正如,匿名函数会在它声明的范围内看到任何东西。因此,函数内部可以看到 map ,它在声明时位于范围内。但在函数之外, marker 是不可见的,所以匿名函数的重复克隆不会相互影响。


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.

解决方案

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.

这篇关于Google Map Infowindow关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆