GeoURL Mapping BookMarklet

Announcing the GeoURL Mapping BookMarklet release 0.1. As mentioned in my last post, I thought it would be neat if you could easily call up a map when you were looking at a web page that had a geocode — that is, a page in which the author had embedded global latitude/longitude coordinates using the conventions outlined by GeoURL or GeoTags. There aren’t all that many pages that are tagged like this, but hopefully more people will be doing it in the future. If the tool finds a tag and can parse it, it opens a new window and loads a MapQuest map centered on the page’s latitude and longitude; otherwise it pops up an alert telling you that it failed.
The JavaScript itself turned out to be not that hard to write, although I have no idea which browsers it works on. Well, I know that when it’s embedded in a test HTML page, it works on Safari and Mozilla Firebird. It just uses W3C DOM, so most newer browsers should support it. The real hassle, however, was getting the javascript to move happily into Safari’s menu bar — boy, it would be cool if browsers gave bookmarklet-type extensions first class support. As it is, you have to monkey around with URL encoding to end up with something like this:
javascript:var%20metatags%20%3D%20document.getElementsByTagName(%22meta%22);var%20geoloc%3Dnull;for(var%20i%3D0;i%3Cmetatags.length;i%2B%2B)%7Bif%20(metatags.item(i).getAttribute(%22name%22))%7Bvar%20name%3Dmetatags.item(i).getAttribute(%22name%22);if((name%3D%3D%22ICBM%22%7C%7Cname%3D%3D%22geo.position%22)%26%26metatags.item(i).getAttribute(%22content%22))%7Bgeoloc%3Dmetatags.item(i).getAttribute(%22content%22);break;%7D%7D%7Dvar%20re%3D/(.%2B)%5B,;%5D(.%2B)/i;if(geoloc)%7Bif(re.test(geoloc))%7Bvar%20lat%3DRegExp.$1;var%20lon%3DRegExp.$2;var%20win%3Dwindow.open();var%20mqURL%3D%22http://www.mapquest.com/maps/map.adp?latlongtype=decimal%26latitude=%22+lat+%22%26longitude=%22+lon+%22%26zoom=8%22;win.document.location=mqURL;%7Delse%7Balert(%22couldnt%20parse%20location:%20%22%20%2B%20geoloc)%7D%7Delse%7Balert(%22no%20location%20meta-tag%20found%22);%7D
Here is the same code without the URL-encoding, in case it helps you read it or otherwise manipulate it:

var metatags = document.getElementsByTagName("meta");
var geoloc=null;
for(var i=0;i<metatags.length;i++){
  if (metatags.item(i).getAttribute("name")){
    var name=metatags.item(i).getAttribute("name");
    if( (name=="ICBM"||name=="geo.position") 
         && metatags.item(i).getAttribute("content")) {
      geoloc=metatags.item(i).getAttribute("content");break;
    }
  }
}
var re=/(\S+)\s*[,;]\s*(\S+)/;if(geoloc){
  if(re.test(geoloc)){
    var lat=RegExp.$1;var lon=RegExp.$2;
    var win=window.open();
    var mqURL="http://www.mapquest.com/maps/map.adp?latlongtype=decimal&latitude="+lat+"&longitude="+lon+"&zoom=8";
    win.document.location=mqURL;
  }else{
    alert("couldnt parse location: " + geoloc)
  }
} else { alert("no location meta-tag found"); }
It works pretty well on GeoURL pages — I have tried a sampling of my neighbors and it works on most that I’ve tried. It failed on the one or two GeoTags pages I tried, but that project seems to have a lot less activity that GeoURL. You are encouraged to use the above, but I make no offer of support at all — the reasons it is likely to break are the most tedious and boring problems to solve. I copied and pasted the above into a Safari Bookmark and it worked; however, I did the same with Mozilla Firebird and it didn’t. Fortunately, I use Safari a lot more than I use Mozilla.

One thought on “GeoURL Mapping BookMarklet

  1. Pingback: Through The Wire

Comments are closed.