Welcome to GNIKville...

Thu, 11 Aug 2005

Adding maps to real estate application

Announcing a Firefox/Greasemonkey script to insert maps into the property listings of www.newenglandmoves.com. The script and a screenshot are here:

http://www.thrall.net/maps/nemoves.html

The interesting thing about this, IMHO, is that I'm loading the gmap javascript files after the page has loaded. Greasemonkey scripts run after the page has loaded so I have to catch the document.write calls and reimplement them via javascript. The relevant code can be found below.

If you like it, feel free to click on one of my ad links ;)


  function loadGmap() {
    GMAP_KEY = YOUR KEY HERE;

    document.write = function(s) {
      var a = s.match(/<script src="(.+)" type="text.javascript">/); 
      if (a) {
        // dodgy... need to let current script load first
        setTimeout('loadScript("'+a[1]+'");', 250);
        return;
      }
      a = s.match(/<style type="text\/css" media="(\w+)">([^<]+)<\/style>/);
      if (a) {
        // can do css right away
        addStyle(a[1], a[2]);
        return;
      }
    }
    loadScript('http://maps.google.com/maps?file=api&v=1&key=' + GMAP_KEY);

  }

  window.loadScript = function(src) {
    var head = document.getElementsByTagName('head')[0];
    var scr = document.createElement('script');
    scr.src = src;
    scr.type = "text/javascript";
    head.insertBefore(scr, head.lastChild.nextSibling);
  }
  window.addStyle = function(media, contents) {
    var head = document.getElementsByTagName('head')[0];
    var style = document.createElement('style');
    style.type = "text/css";
    style.media = media
    style.innerHTML = contents;
    head.insertBefore(style, head.lastChild.nextSibling);
  }


 







Last modified: Fri, Mar 20 02:18:23 2009 GMT