MediaWiki:Common.js: Difference between revisions
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 | ||
document.getElementById('address').innerHTML = json.address. | document.getElementById('address').innerHTML = json.address.town + ',' + json.address.county + ',' + json.address.state + ',' + json.address.country; | ||
} | } | ||
function nominatimsearch(lat,long) { | function nominatimsearch(lat,long) { |
Revision as of 17:42, 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
document.getElementById('address').innerHTML = json.address.town + ',' + json.address.county + ',' + json.address.state + ',' + json.address.country;
}
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);
});