Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I use jQuery or JavaScript inside a Google Maps InfoWindow?
    primarykey
    data
    text
    <p><b>Problem:</b> I want to put Twitter Share Button in my Google Map InfoWindow.</p> <p>I have used a <a href="https://developers.google.com/maps/documentation/javascript/reference#MapsEventListener" rel="nofollow noreferrer">google.maps.event.addListener(infowindow, 'domready' function() { ...code...}</a> to listen for the InfoWindow to be added to the DOM but it only works on the first InfoWindow launched.</p> <p>The <a href="https://dev.twitter.com/docs/tweet-button#Examples" rel="nofollow noreferrer">Twitter Share Button Examples</a> apply the following script:</p> <p><code>!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");</code></p> <p>against the class:</p> <p><code>"twitter-share-button"</code></p> <p><b>Assumption:</b> Now the problem as I see it is that the InfoWindow and markers get created in the $.each() loop and I don't know what event to get jQuery to watch for.</p> <p>I have tried creating a jsfiddle as an example but I can't get it to load the map if I use $.each() like in the live site but I link it here in case it helps you demonstrate an answer:</p> <p>My non-working <a href="http://jsfiddle.net/gyaresu/SmvKC/16/" rel="nofollow noreferrer">JSFiddle</a>.</p> <p>My <strong><a href="http://oslogigs-dev.appspot.com/mobile" rel="nofollow noreferrer">live development site which shows the problem</a>.</strong></p> <p><b>Other Stack Overflow questions: Similar but different.</b></p> <p><a href="https://stackoverflow.com/questions/6089969/calling-a-javascript-function-within-an-infowindow-google-maps">Calling a JavaScript function within an InfoWindow (Google Maps)</a></p> <p><a href="https://stackoverflow.com/questions/9802065/tweet-button-and-infowindow/9808512#9808512">Tweet button and infoWindow</a></p> <p>I've seen the <a href="http://code.google.com/p/jquery-ui-map/wiki/jquery_ui_map_v_3_sample_code" rel="nofollow noreferrer">jQuery-ui-map</a> plugin. That doesn't do anything I can't do in straight JavaScript.</p> <p><b>Map.js:</b> or <a href="http://paste2.org/p/1958148" rel="nofollow noreferrer">Pastebin link to code below but with syntax highlighting</a></p> <pre><code>$('.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 = '&lt;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"&gt;Tweet&lt;/a&gt;'; content_string = '&lt;h1&gt;'+ pub.pub_name + '&lt;/h1&gt;'+ '&lt;p&gt;&lt;a href="http://maps.google.com/?saddr=Current%20Location&amp;daddr=' + pubaddress + '+(' + pubname + ')&amp;dirflg=w&amp;t=h"&gt;Google Maps Directions&lt;/a&gt;&lt;/p&gt;' + '&lt;p&gt;Phone:&lt;a href="tel:' + pub.pub_phone + '"&gt;' + pub.pub_phone + '&lt;/a&gt;&lt;/p&gt;' + '&lt;p&gt;Halvliterpris: NOK &lt;b&gt;' + beer + '&lt;/b&gt;&lt;/p&gt;&lt;p&gt;' + tweet + '&lt;/p&gt;'; 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"); }); }); }); }); </code></pre> <p>Cheers.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload