2012-03-29 2 views
2

Problème: Je veux mettre le bouton Twitter Partager dans ma fenêtre d'information Google Map.Comment utiliser jQuery ou JavaScript dans un InfoWindow Google Maps?

J'ai utilisé un google.maps.event.addListener(infowindow, 'domready' function() { ...code...} pour écouter l'ajout d'InfoWindow au DOM mais cela ne fonctionne que sur la première fenêtre InfoWindow lancée.

Le Twitter Share Button Examples appliquent le script suivant:

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

contre la classe:

"twitter-share-button"

Assomption: Maintenant, le problème que je vois est que le InfoWindow et les marqueurs sont créés dans la boucle $ .each() et je ne sais pas quel événement doit être surveillé par jQuery.

J'ai essayé de créer un fichier jsfiddle à titre d'exemple mais je n'arrive pas à charger la carte si j'utilise $ .each() comme sur le site en ligne mais je le lierai ici au cas où il vous aiderait à répondre :

Mon non-fonctionnement JSFiddle.

Mes live development site which shows the problem.

Autres questions de débordement de pile: Similaire mais différent.

Calling a JavaScript function within an InfoWindow (Google Maps)

Tweet button and infoWindow

J'ai vu le plugin jQuery-ui-map. Cela ne fait rien que je ne puisse pas faire en JavaScript.

Map.js: ou 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"); 
      }); 
     }); 
    }); 
}); 

Vive.

Répondre

4

Appel

twttr.widgets.load(); 

après

bubble.open(map, this); 

devrait faire l'affaire.

Étant donné que vous chargez les widgets Twitter de manière asynchrone, vous pouvez vérifier si twttr est déjà défini, avant de l'appeler.

+1

OH. MON. DIEU. Merci. Maintenant, pourquoi cela ne pourrait-il pas être dans la documentation? :) – Gareth