// GMap LocaPointize!
// Author: Nao Ueda
// Locazing Inc. http://www.locapoint.com
// Version: 1.0 - 10Feb2007 - Initial Release
// License: GNU GPL v2 or later
//
//
// This Code bollows, copyes, and imitates from following User scripts. I really thank these authors.
// UPS Tracking Linkify (http://plutor.org/files/upslinkify.user.js) by Logan Ingalls,
// VoIP Dial Linkify (http://www.muehlen.com/projects/voip/voip_dial.user.js) by Ralf Muehlen,
// and mostly
// Postcode linkify (http://www.qwghlm.co.uk/projects/postcodelinkify.user.js) by Chris Applegate.
//
// Usage:
// Search for a location information ponited by LocaPoint(TM),
// Turn into hyperlink to the relevant location on Google Maps,
// and Add a Title with Latitude/Longitude information
//
// If you are in Google Maps Page, show latitude/longitude and LocaPoint of map center point.
// Also entering LocaPoint in Google Map Serch field guide you that location.
//
// ==UserScript==
// @name AlpsLab LocaPointize!
// @namespace http://www.locapoint.com/
// @description Enable your FireFox for LocaPoint. 1.Add LocaPoint functions on AlpsLab Base. 2.Hyperlink any LocaPoint to AlpsLab Base.
// @include *
// ==/UserScript==
(function () {
//var LPTBMapagent = "GoogleMaps";
var LPTBMapagent = "AlpslabBase";
///// properties
if(!W){
var W = unsafeWindow;
}
if(!W.LocaPointToolbarApplication){
W.LocaPointToolbarApplication = new Object();
}
if(!LPTB){
var LPTB = W.LocaPointToolbarApplication;
if(LPTB.Mapagent && LPTB.Mapagent!=LPTBMapagent){
return;
}else{
LPTB.Mapagent=LPTBMapagent;
}
}
if(!LPTB.LptbTimer){
LPTB.LptbTimer = false;
}
if(!d){
var d = W.document;
}
if(!U){
var U =d.URL;
}
///// functions
///////////////////////////////////////////////////////////////////////////////
//Locapoint2LatLon
///////////////////////////////////////////////////////////////////////////////
LPTB.Locapoint2LatLon = function (locapoint){
var location = new Object();
location.latitude =(
((locapoint.charCodeAt(0)-65)*1757600
+(locapoint.charCodeAt(1)-65)*67600
+(locapoint.charCodeAt(2)-48)*6760
+(locapoint.charCodeAt(8)-65)*260
+(locapoint.charCodeAt(9)-65)*10
+(locapoint.charCodeAt(10)-48))
*180/45697600-90).toFixed(6);
location.longitude = (
((locapoint.charCodeAt(4)-65)*1757600
+(locapoint.charCodeAt(5)-65)*67600
+(locapoint.charCodeAt(6)-48)*6760
+(locapoint.charCodeAt(12)-65)*260
+(locapoint.charCodeAt(13)-65)*10
+(locapoint.charCodeAt(14)-48) )
*360/45697600-180).toFixed(6);
return(location);
}
///////////////////////////////////////////////////////////////////////////////
//LatLon2Locapoint
///////////////////////////////////////////////////////////////////////////////
LPTB.LatLon2Locapoint = function(lat, lon){
var latstep=Math.floor(((lat-(-90))*45697600/180));
var lngstep=Math.floor(((lon-(-180))*45697600/360));
var locapoint=String.fromCharCode(
Math.floor(latstep/1757600%26)+65,
Math.floor(latstep/67600%26)+65,
Math.floor(latstep/6760%10)+48,
46,
Math.floor(lngstep/1757600%26)+65,
Math.floor(lngstep/67600%26)+65,
Math.floor(lngstep/6760%10)+48,
46,
Math.floor(latstep/260%26)+65,
Math.floor(latstep/10%26)+65,
Math.floor(latstep%10)+48,
46,
Math.floor(lngstep/260%26)+65,
Math.floor(lngstep/10%26)+65,
Math.floor(lngstep%10)+48
);
return(locapoint);
}
///////////////////////////////////////////////////////////////////////////////
//FormLatLonStr
///////////////////////////////////////////////////////////////////////////////
LPTB.FormLatLonStr = function(lat, lon){
var latstr =lat>=0?"N":"S";
var lonstr =lon>=0?"E":"W";
lat = Math.abs(lat);
lon = Math.abs(lon);
lat = "000" + lat.toFixed(6);
lon = "000" + lon.toFixed(6);
latstr += lat.substr(lat.length - 9);
lonstr += lon.substr(lon.length - 10);
return(latstr + "," + lonstr);
}
///////////////////////////////////////////////////////////////////////////////
//Wgs84ToTokyo
///////////////////////////////////////////////////////////////////////////////
LPTB.Wgs84ToTokyo = function(wgs84){
var tokyo = new Object();
tokyo.latitude = wgs84.latitude -0 + (0.00010696 * wgs84.latitude) - (0.000017467 * wgs84.longitude) - 0.0046020;
tokyo.longitude = wgs84.longitude -0 + (0.000046047 * wgs84.latitude) -0 + (0.000083049 * wgs84.longitude) - 0.010041;
return(tokyo);
}
///////////////////////////////////////////////////////////////////////////////
//TokyoToWgs84
///////////////////////////////////////////////////////////////////////////////
LPTB.TokyoToWgs84 = function(tokyo){
var wgs84 = new Object();
wgs84.latitude = tokyo.latitude - (0.00010695 * tokyo.latitude) -0 + (0.000017464 * tokyo.longitude) -0 + 0.0046017;
wgs84.longitude = tokyo.longitude - (0.000046038 * tokyo.latitude) - (0.000083043 * tokyo.longitude) -0 + 0.010040;
return(wgs84);
}
///////////////////////////////////////////////////////////////////////////////
//GMapSpy
///////////////////////////////////////////////////////////////////////////////
LPTB.GMapSpy = function(){
//Get Location
//var ll=W.gApplication.getPageUrl().match(/ll=([\+\-\d\.]+),([\+\-\d\.]+)/);
var ll=W.gApplication.getVPageUrlParams().match(/sll=([\+\-\d\.]+),([\+\-\d\.]+)/);
var lp=LPTB.LatLon2Locapoint(ll[1],ll[2]);
var ll_str = LPTB.FormLatLonStr(ll[1], ll[2]);
var fonttag='';
var fonttag2='';
//var fonttag='';
if(d.getElementById('locapointinfo')){
W.locinfo = d.getElementById('locapointinfo');
if(W.locinfo.innerHTML.indexOf(lp,0)==-1){
//update only value changed
W.locinfo.innerHTML = fonttag + lp+' ' + fonttag2 + '('+ll_str+')  ';
}
}else{
//only first time
W.locinfo = d.createElement('span');
W.locinfo.id = 'locapointinfo';
W.locinfo.innerHTML = fonttag + lp+' ' + fonttag2 + '('+ll_str+')  ';
W.node = d.getElementById('print');
W.node.parentNode.insertBefore(W.locinfo, W.node);
}
//Watch Edit Box
var q_str = d.getElementById('q_d').value;
if(q_str.match(/^([A-Z]{2}\d\.){3}[A-Z]{2}\d/)&&(q_str.length==15)){
d.getElementById('q_d').value = LPTB.ReplaceLPinStr(q_str);
}
}
///////////////////////////////////////////////////////////////////////////////
//AlpsBaseSpy
///////////////////////////////////////////////////////////////////////////////
LPTB.AlpsBaseSpy = function(){
//Get Location
var locTokyodatum = new Object();
locTokyodatum.latitude = W._m.getCenterLatLon().lat;
locTokyodatum.longitude = W._m.getCenterLatLon().lon;
var locWgs84 = LPTB.TokyoToWgs84(locTokyodatum);
var lp=LPTB.LatLon2Locapoint(locWgs84.latitude, locWgs84.longitude);
var fonttag='';
if(d.getElementById('locapointinfo')){
W.locinfo = d.getElementById('locapointinfo');
if(W.locinfo.innerHTML.indexOf(lp,0)==-1){
//update only value changed
W.locinfo.innerHTML = 'LocaPoint: ' + fonttag + lp+'  ';
}
}else{
//only first time
W.locinfo = d.createElement('span');
W.locinfo.id = 'locapointinfo';
W.locinfo.innerHTML = 'LocaPoint: '+ fonttag + lp+'  ';
W.node = d.getElementById('latlon_input');
W.node.parentNode.insertBefore(W.locinfo, W.node.parentNode.firstChild);
}
}
///////////////////////////////////////////////////////////////////////////////
//ReplaceLPinStr
///////////////////////////////////////////////////////////////////////////////
LPTB.ReplaceLPinStr = function(in_str){
var ret_str = in_str;
ret_str = ret_str.replace(/([A-Z]{2}\d\.){3}[A-Z]{2}\d/g, function(lp){
var location = LPTB.Locapoint2LatLon(lp);
if(LPTB.Mapagent=="GoogleMaps"){
return(" " + location.latitude + "," + location.longitude + "(" + lp + ") " );
}else if(LPTB.Mapagent=="AlpslabBase"){
if(mapDatum == "Tokyo"){
var locTokyo = LPTB.Wgs84ToTokyo(location);
location.latitude = locTokyo.latitude;
location.longitude = locTokyo.longitude;
}
return(" " + location.latitude + "," + location.longitude);
}
}
);
return(ret_str);
}
///////////////////////////////////////////////////////////////////////////////
//LocaPointLinkify
///////////////////////////////////////////////////////////////////////////////
LPTB.LocaPointLinkify = function(){
//Not a Google Maps / Alps Base
// URL f the map lookup
var mapURL;
var mapDatum="wgs84";
if(LPTB.Mapagent=="GoogleMaps"){
mapURL = "http://maps.google.com/?q=";
}else if(LPTB.Mapagent=="AlpslabBase"){
mapURL = "http://base.alpslab.jp/?p=";
mapDatum = "Tokyo";
}
//
// Locapoint Pattern:"AA0.AA0.AA0.AA0"
const lpRegex = /([A-Z]{2}\d\.){3}[A-Z]{2}\d/g;
//tags we will scan looking for un-hyperlinked urls
var allowedParents = [
"abbr", "acronym", "address", "applet", "b", "bdo", "big", "blockquote", "body",
"caption", "center", "cite", "code", "dd", "del", "div", "dfn", "dt", "em",
"fieldset", "font", "form", "h1", "h2", "h3", "h4", "h5", "h6", "i", "iframe",
"ins", "kdb", "li", "object", "pre", "p", "q", "samp", "small", "span", "strike",
"s", "strong", "sub", "sup", "td", "th", "tt", "u", "var"];
// Xpath statement
var xpath = "//text()[(parent::" + allowedParents.join(" or parent::") + ")]";
// Pick out all text nodes in page that have an allowed parent
var candidates = d.evaluate(xpath, d, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
// For each candidate...
for (var cand = null, i = 0; (cand = candidates.snapshotItem(i)); i++) {
// See if it contains our regexp, if it does...
if (lpRegex.test(cand.nodeValue)) {
// Create a new initially blank node to put in text's place
var span = d.createElement("span");
var source = cand.nodeValue;
cand.parentNode.replaceChild(span, cand);
// Go through each instance
lpRegex.lastIndex = 0;
for (var match = null, lastLastIndex = 0; (match = lpRegex.exec(source)); ) {
// Convert LocaPoint into Latitude and Longitude
var location = LPTB.Locapoint2LatLon(match[0]);
if(mapDatum == "Tokyo"){
var locTokyo = LPTB.Wgs84ToTokyo(location);
location.latitude = locTokyo.latitude;
location.longitude = locTokyo.longitude;
}
// Then create a hyperlink
var a = d.createElement("a");
a.setAttribute("href", mapURL + location.latitude + "," + location.longitude);
a.setAttribute("title", "lat,lon= " + location.latitude + "," + location.longitude);
// Set linktext to be LocaPoint
a.appendChild(d.createTextNode(match[0]));
// Add text between end of previous instance and start of this one
span.appendChild(d.createTextNode(source.substring(lastLastIndex, match.index)));
// Add the hyperlink in
span.appendChild(a);
// Remember the end index of this instance
lastLastIndex = lpRegex.lastIndex;
}
// Add the rest of the text in
span.appendChild(d.createTextNode(source.substring(lastLastIndex)));
span.normalize();
// All done!
}
}
return;
}
///////////////////////////////////////////////////////////////////////////////
// Main Logic
///////////////////////////////////////////////////////////////////////////////
if(U.match(/google/) && U.match(/local|map/) && (W.gApplication)){
//if Google Maps Page
var href_str=d.getElementById('link').href;
if(href_str.match(/(q\=)([A-Z]{2}\d\.){3}([A-Z]{2}\d)/)){
//W.alert("Gmap & has LocaPoint query");
//if "q=xxxxx" contains LocaPoint, convert to coordinates and call G-Map again
href_str = LPTB.ReplaceLPinStr(href_str);
//W.alert(href_str);
W.location.href=href_str;
clearInterval(LPTB.LPtim); //
}else{
//if G-Map, get Center Location periodically
//W.alert("Gmap");
LPTB.GMapSpy();
if(LPTB.LptbTimer==false){
LPTB.LPtim = W.setInterval("window.LocaPointToolbarApplication.GMapSpy();",200);
LPTB.LptbTimer = true;
}
}
}else if(U.match(/base/) && U.match(/alpslab/)){
//if AlpsLabBase Page
//Location periodically
//LPTB.AlpsBaseSpy();
if(LPTB.LptbTimer==false){
LPTB.LPtim = W.setInterval("window.LocaPointToolbarApplication.AlpsBaseSpy();",200);
LPTB.LptbTimer = true;
}
}else{
LPTB.LocaPointLinkify();
}
})();