MediaWiki:Common.js: Difference between revisions

From ponti
Jump to navigation Jump to search
No edit summary
No edit summary
Line 3: Line 3:
function cb(json) {
function cb(json) {
     //do what you want with the json
     //do what you want with the json
var address;
address = '';
if (typeof json.address.town != 'undefined') address += json.address.town + ',';
if (typeof json.address.town != 'undefined') address += json.address.town + ',';
if (typeof json.address.city != 'undefined') address += json.address.city + ',';
if (typeof json.address.city != 'undefined') address += json.address.city + ',';

Revision as of 16:49, 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.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 + ',';

    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);
});