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.

Analyse text and add a map with its locations in pure JavaScript by  you.

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: , , , , ,

5 Responses to “Adding map links and a small map to any text using JavaScript – addmap.js”

  1. jcoglan (James Coglan) Says:






    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

  2. Keith Says:

    Awesome script Chris!

    I have added it as a user macro for use in Confluence here.

  3. David Votrubec Says:

    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?

  4. Stephen Says:

    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?

  5. Stephen Says:

    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!

Wait till I come! is the blog of Christian Heilmann , a developer evangelist living and working in London, England. Download vcard.

Feed me, Seymour: Entries (RSS) and Comments (RSS).