As I’m often switching location between my home in the UK and working out in Redmond, I’ve been thinking for a while about how best to easily let people know where I am. As you can hopefully see, I’ve added a map to every page on this blog and I thought that maybe people might be interested how I did this, as I learnt a few things on the way that were reasonably interesting.
The first challenge was to figure out the best way around the limitations of adding a map to the blog page. Although the Community Server software that this site runs on is pretty flexible, you can only add elements to every page in various sections – in my case in the News section. Luckily there don’t appear to be any restrictions on the actual HTML you can add, so I was left with a choice of options.
My first thought was to add an <IFRAME… /> section that would call out to a page on my own server that would use Windows Live Maps to display where I am by adding a pushpin at the appropriate location. However this didn’t really work very well, as the map code didn’t really like being in an IFrame and the flow of the page around the map was broken.
Therefore the solution I hit on was to add the following code:
The ‘myMap’ element is the div where the map will be placed by the JavaScript that I create on my server. Note that I am passing the name of this element into my script so I can use it.
Interesting point to note is adding the defer=true on to the script call. Without it, I was occasionally hitting a known issue in Internet Explorer due to a bug in the IE parser :-(
The ASPX page that generates the JavaScript is shown below. I won’t show the pretty simple code behind just yet, as I’m hoping to add more functionality to this soon before I share the code. However in this initial version it just creates a “LocationEntry” object from an XML file held on the server, and then uses this object to set various parts of the script.
The interesting things of note in the JavaScript are:
Hopefully this is pretty self-explanatory – see http://dev.live.com/virtualearth/ for more details about the Virtual Earth API.
Here’s the ASPX/JavaScript code:
Future plans (which I suspect are overly ambitious!) for this application involve: