2012-03-29 50 views
2

問題:我想把Twitter分享按鈕放到我的Google Map InfoWindow中。如何在Google Maps InfoWindow中使用jQuery或JavaScript?

我已經使用google.maps.event.addListener(infowindow, 'domready' function() { ...code...}來偵聽要添加到DOM的InfoWindow,但它只能在啓動的第一個InfoWindow上工作。

Twitter Share Button Examples應用下面的腳本:

​​

反對階級:

"twitter-share-button"

假設: 現在的問題,我看到它是一個信息窗口和標記是在$ .each()循環中創建的,我不知道要讓jQuery觀察什麼事件。

我曾嘗試創建一個jsfiddle作爲示例,但如果我使用$ .each()就像在活動網站中那樣加載地圖,但我將它鏈接到此處以防它幫助您演示答案:

我的非工作JSFiddle

我的live development site which shows the problem

其他堆棧溢出問題:類似但不同。

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

乾杯。

回答

4

調用

twttr.widgets.load(); 

bubble.open(map, this); 

應該做的伎倆。

由於您正在異步加載Twitter小部件JavaScript,因此在調用它之前,您可能需要檢查是否已經定義了twttr

+1

OH。 MY。神。謝謝。現在爲什麼不能在文檔中? :) – Gareth 2012-03-31 07:13:05

相關問題