Adding map links and a small map to any text using JavaScript – addmap.js
As part of a larger article, I am currently building some tools that use geographical information. The first one is a pure JavaScript solution to link locations in a text to Google Maps and to show a small map under the text. You can see it in action by clicking the screenshot below.
All you need to do to use the script is “get your own Google Maps key”:http://code.google.com/apis/maps/signup.html and embed the script in the page you want to analyse, giving it the ID of the main text element as a parameter:
<script src="http://github.com/codepo8/geotoys/raw/master/addmap.js"></script>
<script>
addmap.config.mapkey = 'YOUR_API_KEY';
addmap.analyse('content');
</script>
You can customise the look and feel by writing your own CSS for the generated HTML and setting the addmap.config.width and addmap.config.height properties to resize the map.
Under the hood here is what happens:
- The script grabs the innerHTML of the element and runs it through the open YQL table of “Yahoo Placemaker”:http://developer.yahoo.com/geo/placemaker.
- It then takes the data, assembles the HTML and puts together the URI for the map
- The map gets displayed using the “Google static map API“:http://gmaps-samples.googlecode.com/svn/trunk/simplewizard/makestaticmap.html
The “code of addmap.js”:http://github.com/codepo8/geotoys is available on GitHub.
Tags: geo, googlemaps, javascript, maps, placemaker, yql



January 29th, 2010 at 3:58 pm
RT @codepo8: Adding a map with the locations in any text to the text using a line of JavaScript – addmap.js [link to post]
– Posted using Chat Catcher
January 29th, 2010 at 6:45 pm
Awesome script Chris!
I have added it as a user macro for use in Confluence here.
February 8th, 2010 at 11:20 am
Hi,
the demo page looks great, i like it a lot, but for some reason it doesnt work on my site.
I have my own GMamps API key.
Can you help please?
February 11th, 2010 at 9:51 pm
I too tried this out but get errors… o.query.results is null inside here:
function paint(o){
var results = o.query.results.matches.match;
…
Do I need a YQL key too?
February 11th, 2010 at 10:13 pm
Ah, figured it out…
Running the query against Yahoo was failing my permission check as I was running from my localhost 127.0.0.1 and I wasn’t signing my request. I’ll have to post online and re-try…
thanks!