Showing posts with label Google Maps. Show all posts
Showing posts with label Google Maps. Show all posts

Monday, July 19, 2010

Dojo templates & Google Maps InfoWindow

I have been building mashups using Google Maps since 2007 and one problem I had is passing in the content for these bubbles that show up when you click on a marker i.e. the InfoWindow. One big annoyance with it, is that you have to pass in the HTML content as a string when opening the marker. I don't like it because I have to intertwine HTML inside of JavaScript. So how can we make this better?

Dojo Templates


Dojo Templates is the number one reason I love this library, I like the PubSub mechanism I posted about last time but not as much as the templates.

Dojo Templates allow you to associate HTML template files with your widgets, that get instantiated when Dojo parses your page and constructs the widget, basically replacing the references to your widgets with the widget's markup in the HTML template file.

First: my infowindow.html template file

<div class="infoWindowContainer" dojoattachpoint="infoWindow">
<h1>${title}</h1>
</div>

Don't worry about that ${title} thing just yet, but I guess you already can see where I am going with this.

Second: my infowindow.js widget

dojo.declare(
"nael.widgets.infowindow",
[dijit._Widget, dijit._Templated],
{
templatePath: new dojo.moduleUrl('nael.templates', 'infowindow.html'),
constructor: function(){

}
}
);

In this example, my infowindow widget's constructor is empty.

Next we create the marker. Here we using dojo.forEach to loop over all the points that were returned with our AJAX response to fetch points. this.map is the object within this map controller that references the Google Map. (This is the same controller from the previous post on Dojo PubSub mechanism)

dojo.forEach(pois, dojo.hitch(this, function(p){
var marker = new google.maps.Marker({
position: new google.maps.LatLng(p.lat, p.lng),
map: this.map
});
var content = "<div class='infoWindow' dojoType='nael.widgets.infowindow' title='"+p.Pois.name+"'>
" ;
this.addInfoWindowToMarker(marker,content);
}));


The important part is the line where we add the content. Yes, we still need markup in there, but now its just a placeholder for the real markup. We can pass attributes like title into the placeholder for nael.widgets.infowindow. So anything that you want displayed in the info window becomes an attribute. This allows you to focus on content, and not worry about presentation just yet.

The last method we call addInfoWindowToMarker creates a Google maps listener on the marker and connects the info window to it. Note, that the info window here, is not the widget we created in the beginning. The one at the top is "nael's infowindow" and only serves the purpose of templating.

addInfoWindowToMarker: function(marker,content){
google.maps.event.addListener(marker, 'click', dojo.hitch(this, function(){
this.infoWindow.content = content;
this.infoWindow.maxWidth = 300;
this.infoWindow.open(this.map, marker);
}));
}


If you try the above, and click on the marker, the content will still be empty. Because this is a widget, it needs to be constructed. You need to tell Dojo when to parse the DOM to look for new widgets that you introduced since the last parse.


google.maps.event.addListener(this.infoWindow, "domready", dojo.hitch(this, function(){
dojo.parser.parse(this.mapCanvasNode.id);
}));


We don't want Dojo to go looking through the whole DOM for new widgets, we know where the widget was added. So we can just tell Dojo to look for widgets within the div referenced by the HTML id this.mapCanvasNode.id)

Finally, back to our infowindow.html template:

<div class="infoWindowContainer" dojoattachpoint="infoWindow">
<h1>${title}</h1>
</div>


We can now adjust the template as we please without trouble, and this sure is much cleaner than doing this like I used to for years.

var content = "<div class='infoWindowContainer'>";
content += "<h1>" + title + "</h1>";
content += "</div>";


Maybe one day Google Maps will support templating the HTML for InfoWindows internally, until then, I'm sticking to the above when I can.



The benefit of templating the InfoWindow becomes obvious when you are dealing with complex InfoWindows with functionality built into it such as sharing on social networks, embedded videos, AJAX requests, pictures, etc. All that stuff can be templated, and only the dynamic stuff that comes from the backend is passed through.

Of course, on top of being able to template your InfoWindow markup, it is much easier now to replace an InfoWindow with a version 2.0 of the InfoWindow. You just have to drop in the new and improved widget and template, then abide by the Pub Sub channels you have defined between the widget and the rest of the application.

Saturday, April 10, 2010

First Google Maps Mashup Turns 5 Years

Last Thursday the Google Maps mashup turned 5. Paul Rademacher posted this on Craigslist announcing a Google map mashup of housing properties. That post started it all. Back then, there wasn't even an official API for Google Maps. That mashup now lives at housingmaps.com.

Congrats to Paul on this milestone. That experiment started the whole mashup building community, and the reason why Google Maps has an API today and why we have sites like zoocasa, University of Ottawa Campus map, twittervision, and many more mashups built on top of the Google Maps API.

If you are interested in building a Google Maps mashup, get in touch with ThinkWrap's Google Maps Gurus.

Saturday, October 17, 2009

Toronto Google Street View Time-Lapse

Its pretty exciting that Google Street View has finally launched across several Canadian cities.


What is more exciting, is finding myself standing outside the ThinkWrap Solutions office!


View Larger Map

Thursday, April 30, 2009

Recursive Search in Google Maps - Why is it interesting?

Apparently this has been out for some time now, but I am able to run recursive searches on Google Maps. I have used the "What is nearby" searches, but I was unaware I can do this from the search field.

What is a typical search on Google Maps?
Obviously the most common would be something like "2000 Main St. Hamilton, ON"

Then there is a slightly more interesting search query...
something like "Coffee in Toronto" is something typical to have on a location map


Finally, there is the ultimate - so far - location query; the recursive query
for example "Coffee near highway in Toronto"


I like it, I like it a lot actually. 

So how about we use some nested recursion?
"Tim Horton's near airport and Starbucks"


Now this is getting really interesting, you can now see the bunch of points around Pearson and another bunch around the Island and some orphaned point by Scarborough. This is beyond finding directions, one could use this for some analysis on where specific brands focus their stores, or even which regions in a city different brands clash ex. Tim Horton and Starbucks. Actually it would be useful if Google Maps colored each marker differently in the above recursive search, ex. red vs. blue. This way I can actually see where Tim Horton and Starbucks are throwing punches at each other.

Let me just check one of these locations, and make sure there is actually a Starbucks near the Timmies - just to make sure Google Maps is not pulling my leg here.

I picked that point marked "A" on the very left, and searched nearby locations...


and sure enough that Tim Horton is near a Starbucks and the airport. You can even click the markers to view the info windows and verify this yourself.

So next time you use Google Maps remember you can do more than just a location search.



Monday, April 27, 2009

Track Swine Flu On Google Maps

Google Henry Niman has published a Google Maps application to track the recent Swine Flu outbreak around the World. Google Google Maps platform is a great way to quickly publish applications regarding current events.

For a Swine Flu tracking application I would like to see someone do any of the following:

  • add a timeline and be able to view how the disease spread, just like the Google News Timeline launched at Google Labs
  • notification via e-mail or twitter if there was a case reported around your location
  • be able to compare the patterns of this outbreak with that of SARS or some other disease
Comment if you know of any application that does any of the above.

Tuesday, December 30, 2008

Computer breakdowns, Google Reader, TimeSpace and goodbye 2008

Once again I find myself neglecting this blog, lets see if 2009 is a better year.
So my PC laptop crashed at home last week, and after many many attemps I managed to boot it into safe mode just long enough to drag all my stuff out onto another drive. I loaded all my photos, university stuff, master's project and papers, etc. on to my G4 powerbook last night and everything seemed like it was working fine. I "needed" to run my mac updates since I have not done so in a while, but really I should not have as something went coocoo after that update - maybe just a coincidence but I'm pretty sure the hard drive is pooched. So back to my PC laptop which I spent 2 hours this evening restoring and now it is in pretty good shape, much much faster than before which is one good thing that came out of this. But now I have this pretty g4 laptop that is pretty much useless until I figure out if I need a new hard drive or not, and whether I can grab the stuff inside or not. I do have a backup of it, but my only concern is I am not sure how recent my backup of the code I wrote for my masters is. Sure that was a while back (Jan 2008) but still I would like to keep the code I delivered...

Anyway enough boringness, for the first time I set up iGoogle and personalize my page. Very neat, added a lot of fancy little portlets, got my news, jokes, weather and shit all on one page. But then I decided to setup Google Reader and hook it to the blogs I like to keep up to date on. Now that Google Reader is a master piece - so simple yet I think it is probably one of the most useful product to come out of Google Labs. I love it! Thanks to Google Reader, I came across TimeSpace - a Google map based news aggregator with a time slider

Very cool. As you can see I have been occupied with the news from Gaza, what a sad way to end 2008. God help them and bless them. Anyway won't start my rant on that topic just yet...

Wednesday, August 01, 2007

Deja vu!


I was checking out findbyclick.com (a community to share map locations and points of interest using the Google Maps application), and I decided to check out the location for Pentura Solutions where I started working this week. To my surprise I found a black car parked right behind the office in an almost empty section of the lot and I thought "Deja vu!" This was the exact same view I saw today when I left the office at 6pm today, but from the top. Check it out, you never know what you might find other than the gazillion Tim Horton's and Starbucks locations already posted!