javascript - 如何在 Google map 信息窗口中使用 jQuery 或 JavaScript?

标签 javascript jquery google-maps

问题:我想将 Twitter 分享按钮放入我的 Google map 信息窗口中。

我用过google.maps.event.addListener(infowindow, 'domready' function() { ...code...}监听要添加到 DOM 的 InfoWindow,但它仅适用于第一个启动的 InfoWindow。

Twitter Share Button Examples应用以下脚本:

!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s) ;js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(文档,"脚本","twitter-wjs");

针对类(class):

“twitter-share-button”

假设: 现在我看到的问题是 InfoWindow 和标记是在 $.each() 循环中创建的,我不知道要让 jQuery 监视什么事件。

我尝试创建一个 jsfiddle 作为示例,但如果我像在实时站点中一样使用 $.each() ,则无法让它加载 map ,但我将其链接到此处,以防它可以帮助您演示答案:

我的非工作JSFiddle .

我的 live development site which shows the problem .

其他 Stack Overflow 问题:类似但不同。

Calling a JavaScript function within an InfoWindow (Google Maps)

Tweet button and infoWindow

我已经看到了jQuery-ui-map插入。这并没有做任何我在直接 JavaScript 中做不到的事情。

Map.js:Pastebin link to code below but with syntax highlighting

$('.page-map').live("pageshow", function() {

    $.getJSON("/rest/Pub", function(data) {

        var latlng = new google.maps.LatLng(59.920, 10.750);  
        var map = new google.maps.Map(document.getElementById("map-canvas"), {
                // zoom is good on iphone @ 13 but better on desktop @ 14
                zoom: 12,
                center: latlng,
                backgroundColor: 'black',
                mapTypeControlOptions: { 
                    style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
                },
                mapTypeId: google.maps.MapTypeId.HYBRID
        }); 
        var bubble = new google.maps.InfoWindow();
        var pubs = data.list.Pub;

        $.each(pubs, function(i, pub) {

            var beer;
            var pubaddress = encodeURI(pub.pub_address);
            var pubname = encodeURI(pub.pub_name);
            var posi = new google.maps.LatLng(pub.pub_lat, pub.pub_long);
            var marker = new google.maps.Marker({
                animation: (pub.pub_bounce =='true') ? google.maps.Animation.BOUNCE : null,
                map: map,
                icon: "/static/images/" + pub.beer_price + ".png",
                shadow: 'static/images/shadow.png',
                position: posi,
                title: pub.pub_name
            });

            if (pub.beer_price == 99) {
                marker.setZIndex(1);
            } else {
                marker.setZIndex(100);
            }

            google.maps.event.addListener(marker, 'click', function() {
                // http://mapki.com/wiki/Google_Map_Parameters
                    if (pub.beer_price == 99) {
                        beer = '?';
                    } else {
                        beer = pub.beer_price; 
                    }

                tweet = '<a href="https://twitter.com/share" class="twitter-share-button" data-url="http://oslogigs.com" data-text="' + pub.pub_name + '" data-lang="en" data-via="OsloGigs" data-hashtags="oslo, oslogigs, norway">Tweet</a>';

                content_string = '<h1>'+  pub.pub_name + '</h1>'+
                '<p><a href="http://maps.google.com/?saddr=Current%20Location&daddr=' + 
                pubaddress + '+(' + pubname + ')&dirflg=w&t=h">Google Maps Directions</a></p>' +
                '<p>Phone:<a href="tel:' + pub.pub_phone + '">' + pub.pub_phone + '</a></p>' +
                '<p>Halvliterpris: NOK <b>' + beer + '</b></p><p>' + tweet + '</p>';


            bubble.setContent(content_string);
            console.log(bubble.getContent());
            bubble.open(map, this);
            });

            google.maps.event.addListener(bubble, 'domready', function() {
            //$('.twitter-share-button').on('domready', function(event) {
               console.log("Iteration:"+ i + " Twitter javascript loaded " + pub.pub_name);
               !function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");
            });
        });
    });
});

干杯。

最佳答案

调用

twttr.widgets.load();

之后

bubble.open(map, this);

应该可以解决问题。

由于您要异步加载 Twitter 小部件 JavaScript,因此您可能需要在调用之前检查 twttr 是否已定义。

关于javascript - 如何在 Google map 信息窗口中使用 jQuery 或 JavaScript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9924296/

相关文章:

javascript - 单击按钮时打开空白窗口

android - 谷歌地图 Android Circle 填充不起作用

javascript - 使用AJAX加载跨域端点

javascript - 连接到表单提交 Node js上的套接字

javascript - 将一个子元素居中,同时将其他子元素保持在原处。

javascript - 如何在javascript中的onclick函数之外传递这个关键字

javascript - 避免与固定元素重叠

android - 如何根据在 map 上绘制的圆圈找到缩放级别

google-maps - 如何在 JasperReport map 上显示标签和自定义图标?

javascript - 如何按顺序执行 Node JS 函数