Am trying to laod tweets into a div after looping them from yahoo placemaker. They are loading on the div but the information shown by them is placemaker’s last result.
This is the code..
function getLocation(user, date, profile_img, text,url) {
var templates = [];
templates[0] = '<div><div></div><h2 class="firstHeading">'+user+'</h2><div>'+text+'</div><div><p><a href="' + url + '"target="_blank">'+url+'</a></p></div><p>Date Posted- '+date+'</p></div>';
templates[1] = '<table width="320" border="0"><tr><td class="user" colspan="2" rowspan="1">'+user+'</td></tr><tr><td width="45"><a href="'+profile_img+'"><img src="'+profile_img+'" width="55" height="50"/></a></td><td width="186">'+text+'<p><a href="' + url + '"target="_blank">'+url+'</a></p></td></tr></table><hr>';
templates[2] = '<div><div></div><h2 class="firstHeading">'+user+'</h2><div>'+text+'</div><div><p><a href="' + url + '"target="_blank">'+url+'</a></p></div><p>Date Posted- '+date+'</p></div>';
templates[3] = '<table width="320" border="0"><tr><td class="user" colspan="2" rowspan="1">'+user+'</td></tr><tr><td width="45"><a href="'+profile_img+'"><img src="'+profile_img+'" width="55" height="50"/></a></td><td width="186">'+text+'<p><a href="' + url + '"target="_blank">'+url+'</a></p></td></tr></table><hr>';
var geocoder = new google.maps.Geocoder();
Placemaker.getPlaces(text, function (o) {
console.log(o);
if (!$.isArray(o.match)) {
var latitude = o.match.place.centroid.latitude;
var longitude = o.match.place.centroid.longitude;
var myLatLng = new google.maps.LatLng(latitude, longitude);
var marker = new google.maps.Marker({
icon: profile_img,
title: user,
map: map,
position: myLatLng
});
var infowindow = new google.maps.InfoWindow({
content: templates[0].replace('user',user).replace('text',text).replace('url',url).replace('date',date)
});
var $tweet = $(templates[1].replace('%user',user).replace(/%profile_img/g,profile_img).replace('%text',text).replace('%url',url));
$('#user-banner').css("visibility","visible");$('#news-banner').css("visibility","visible");
$('#news-tweets').css("overflow","scroll").append($tweet);
function openInfoWindow() {
infowindow.open(map, marker);
}
google.maps.event.addListener(marker, 'click', openInfoWindow);
$tweet.find(".user").on('click', openInfoWindow);
bounds.extend(myLatLng);
}
});
}
ak,
After some experimentation, I tracked down the problem to
Placemaker.jsnot being able to handle multiple simultaneous requests. It’s tempting to think you can overcome the problem with closures to remember loop-generated data but this doesn’t work. The fix needs to be withinPlacemakerand I opted to refactor it as a jQuery plugin. jQuery affords the possibility of returning apromisefrom thegetPlacesmethod, making it similar to jQuery’s native$.ajax(),$.get()etc.Yahoo! Placemaker.js as a jQuery Plugin
Setting the appID
Note that
.Placemaker()needs to be invoked on a jQuery object. For the ‘config’ method, any selector will do, so an empty jQuery object,$()will suffice.Using the Plugin
The rest of your code, including a plugin call, will look like this :
At the heart of this, you will find the following structure :
It is important to note that :
.Placemaker('getPlaces', d), methods in the chain return a jQuery promise object..done()and the.fail()handler,thisis equivalent to the original standard jQuery object..Placemaker('getPlaces', d), reappears as the second argument of the.done()handler. This feature allows us to call.Placemaker('getPlaces', d)in a for loop without needing to specifically put the data into a closure or storing it in the DOM with.data(). In this regard, the ‘getPlaces’ method effectively serves as a closure as well as providing the required asynchronous lookup behaviour.All this will be judged as thoroughly confusing or totally clever depending on your point of view.