MediaWiki:Common.js: Difference between revisions

From ponti
Jump to navigation Jump to search
No edit summary
No edit summary
 
(20 intermediate revisions by the same user not shown)
Line 3: Line 3:
function cb(json) {
function cb(json) {
     //do what you want with the json
     //do what you want with the json
    document.getElementById('output').innerHTML = json.address.road + ',' + json.address.city;
var address;
address = '';
if (typeof json.address.village != 'undefined') address += json.address.village + ', ';
if (typeof json.address.town != 'undefined') address += json.address.town + ', ';
if (typeof json.address.city != 'undefined') address += json.address.city + ', ';
if (typeof json.address.county != 'undefined') address += json.address.county + ', ';
if (typeof json.address.state != 'undefined') address += json.address.state + ', ';
if (typeof json.address.country != 'undefined') address += json.address.country;
 
    document.getElementById('address').innerHTML = address;
}
}
function nominatimsearch(lat,long) {
function nominatimsearch(lat,long) {
Line 11: Line 20:
};
};


function coord2loc(e, n)
function chr(x){
// calculate IARU locator from lat, lon
return String.fromCharCode(x);
// includes correction to hopefully avoid js fp maths error.
}
// input is in degrees.
 
{
function floor(x){
e = e + 180;
return Math.floor(x);
ee = e;
}
n = n + 90;
 
nn = n;
function coord2loc(lat, long){
var locator = "";
var ychr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
e = e / 20 + 0.0000001;
      var ynum = "0123456789";
n = n / 10 + 0.0000001;
      var yqth, yi, yk, ydiv, yres, ylp, y;
locator = locator + chr(65 + e) + chr(65 + n);
      var y = 0;
e = e - floor(e);
      var ycalc = new Array(0,0,0);
n = n - floor(n);
      var yn = new Array(0,0,0,0,0,0,0);
e = e * 10;
 
n = n * 10;
      ycalc[1] = long + 180;
locator = locator + chr(48 + e) + chr(48 + n);
      ycalc[2] = lat +  90;
e = e - floor(e);
 
n = n - floor(n);
      for (yi = 1; yi < 3; ++yi) {
e = e * 24;
        for (yk = 1; yk < 4; ++yk) {
n = n * 24;
            if (yk != 3) {
locator = locator + chr(65 + e) + chr(65 + n);
            if (yi == 1) {
e = e - floor(e);
              if (yk == 1) ydiv = 20;
n = n - floor(n);
              if (yk == 2) ydiv = 2;
e = e * 10;
            }
n = n * 10;
            if (yi == 2) {
locator = locator + chr(48 + e) + chr(48 + n);
              if (yk == 1) ydiv = 10;
e = e - floor(e);
              if (yk == 2) ydiv = 1;
n = n - floor(n);
            }
e = e * 24;
 
n = n * 24;
            yres = ycalc[yi] / ydiv;
locator = locator + chr(65 + e) + chr(65 + n);
            ycalc[yi] = yres;
return locator;
            if (ycalc[yi]>0)
              ylp = Math.floor(yres)
            else
              ylp = Math.ceil(yres);
            ycalc[yi] = (ycalc[yi] - ylp) * ydiv;
            }
            else {
            if (yi == 1)
              ydiv = 12
            else
              ydiv = 24;
 
            yres = ycalc[yi] * ydiv;
            ycalc[yi] = yres;
            if (ycalc[yi] > 0)
              ylp = Math.floor(yres)
            else
              ylp = Math.ceil(yres);
            }
 
        ++y;
        yn[y] = ylp;
        }
      }
 
      yqth = ychr.charAt(yn[1]) + ychr.charAt(yn[4]) + ynum.charAt(yn[2]);
      yqth += ynum.charAt(yn[5]) + ychr.charAt(yn[3])+ ychr.charAt(yn[6]);
return yqth;
}
}


Line 51: Line 87:
var coordinates = document.getElementById("locator").innerHTML;
var coordinates = document.getElementById("locator").innerHTML;
var splitcoord = coordinates.split(",");
var splitcoord = coordinates.split(",");
var latitude = parseFloat(splitcoord[0].trim().slice(0,-1));
var longitude = parseFloat(splitcoord[1].trim().slice(0,-1));


document.getElementById("locator").innerHTML = splitcoord[0].trim().slice(0,-1);
document.getElementById("locator").innerHTML = coord2loc(latitude ,longitude);
 
nominatimsearch(latitude ,longitude);
 
});
});

Latest revision as of 17:21, 3 December 2014

/* Any JavaScript here will be loaded for all users on every page load. */

function cb(json) {
    //do what you want with the json
var address;
address = '';
if (typeof json.address.village != 'undefined') address += json.address.village + ', ';
if (typeof json.address.town != 'undefined') address += json.address.town + ', ';
if (typeof json.address.city != 'undefined') address += json.address.city + ', ';
if (typeof json.address.county != 'undefined') address += json.address.county + ', ';
if (typeof json.address.state != 'undefined') address += json.address.state + ', ';
if (typeof json.address.country != 'undefined') address += json.address.country;

    document.getElementById('address').innerHTML = address;
}
function nominatimsearch(lat,long) {
    var s = document.createElement('script');
    s.src = 'http://nominatim.openstreetmap.org/reverse?json_callback=cb&format=json&lat='+lat+'&lon='+long+'&addressdetails=1';
    document.getElementsByTagName('head')[0].appendChild(s);
};

function chr(x){
 return String.fromCharCode(x);
}

function floor(x){
	return Math.floor(x);
}

function coord2loc(lat, long){
 var ychr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
      var ynum = "0123456789";
      var yqth, yi, yk, ydiv, yres, ylp, y;
      var y = 0;
      var ycalc = new Array(0,0,0);
      var yn = new Array(0,0,0,0,0,0,0);

      ycalc[1] = long + 180;
      ycalc[2] = lat +  90;

      for (yi = 1; yi < 3; ++yi) {
        for (yk = 1; yk < 4; ++yk) {
            if (yk != 3) {
            if (yi == 1) {
              if (yk == 1) ydiv = 20;
              if (yk == 2) ydiv = 2;
            }
            if (yi == 2) {
              if (yk == 1) ydiv = 10;
              if (yk == 2) ydiv = 1;
            }

            yres = ycalc[yi] / ydiv;
            ycalc[yi] = yres;
            if (ycalc[yi]>0)
              ylp = Math.floor(yres)
            else
              ylp = Math.ceil(yres);
            ycalc[yi] = (ycalc[yi] - ylp) * ydiv;
            }
            else {
            if (yi == 1)
              ydiv = 12
            else
              ydiv = 24;

            yres = ycalc[yi] * ydiv;
            ycalc[yi] = yres;
            if (ycalc[yi] > 0)
              ylp = Math.floor(yres)
            else
              ylp = Math.ceil(yres);
            }

        ++y;
        yn[y] = ylp;
        }
      }

      yqth = ychr.charAt(yn[1]) + ychr.charAt(yn[4]) + ynum.charAt(yn[2]);
      yqth += ynum.charAt(yn[5]) + ychr.charAt(yn[3])+ ychr.charAt(yn[6]);
return yqth;
}

$(window).bind("load", function() {
   // code here
var coordinates = document.getElementById("locator").innerHTML;
var splitcoord = coordinates.split(",");
var latitude = parseFloat(splitcoord[0].trim().slice(0,-1));
var longitude = parseFloat(splitcoord[1].trim().slice(0,-1));

document.getElementById("locator").innerHTML = coord2loc(latitude ,longitude);
nominatimsearch(latitude ,longitude);
});