MediaWiki:Geolinks-US-hoodscale.js

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
// Add map service links from [[Template:Geolinks-US-hoodscale]] to [[Template:Location]] and [[Template:Location dec]] on image pages

if (mw.config.get('wgNamespaceNumber') == 6 && mw.config.get('wgAction') == 'view') {
	$(document).ready(add_geolinks_US);
}

function walkDOM_uptoTagName(startElement, endElementTagName) {
	var el = startElement;
	while (el.parentNode) {
		el = el.parentNode;
		if (el.tagName == endElementTagName)
			return el;
	}
	return false;
}

function walkDOM_righttoTagName(startElement, endElementTagName) {
	var el = startElement;
	while (el.nextSibling) {
		el = el.nextSibling;
		if (el.tagName == endElementTagName)
			return el;
	}
	return false;
}

function add_geolinks_US() {
	var spans = document.getElementsByTagName('span');
	for (var i=0; i<spans.length; i++) {
		if (spans[i].className === 'geo') {
			var geo = spans[i];

			var geospans = geo.getElementsByTagName('span');
			var latitude = 0;
			var longitude = 0;
			if (geospans.length>0) { // location dec
				for (var j=0; j<geospans.length; j++) {
					if (geospans[j].className == 'latitude') {
						latitude = geospans[j].innerHTML;
					} else if (geospans[j].className == 'longitude') {
						longitude = geospans[j].innerHTML;
					}
				}
			} else { // location
				var latlon = geo.innerHTML.split(';');
				latitude = latlon[0];
				longitude = latlon[1];
			}

			var geocell = walkDOM_uptoTagName(geo, 'TD');
			if (!geocell)
				return false;
			
			var nextcell = walkDOM_righttoTagName(geocell, 'TD');
			if (!nextcell)
				return false;
				
			nextcell.innerHTML += '<ul>\
<li>Street map from <a href="http://mapquest.com/maps/map.adp?latlongtype=decimal&amp;latitude='+latitude+'&amp;longitude='+longitude+'&amp;zoom=7" class="external text">MapQuest</a> or <a href="http://maps.google.com/maps?ll='+latitude+','+longitude+'&amp;spn=0.03,0.045" class="external text">Google Local</a></li>\
<li>Topographic map from <a href="http://topozone.com/map.asp?lat='+latitude+'&amp;lon='+longitude+'&amp;s=50&amp;size=m&amp;datum=nad83" class="external text">TopoZone</a></li>\
<li>Aerial image or topographic map from <a href="http://msrmaps.com/image.aspx?s=13&amp;lon='+longitude+'&amp;lat='+latitude+'&amp;w=2" class="external text">MSR Maps (formerly TerraServer-USA)</a></li>\
<li>Satellite image from <a href="http://maps.google.com/maps?ll='+latitude+','+longitude+'&amp;spn=0.03,0.045&amp;t=k" class="external text">Google Maps</a> or <a href="http://virtualearth.msn.com/default.aspx?cp='+latitude+'|'+longitude+'&amp;style=h&amp;lvl=14&amp;v=1" class="external text">Microsoft Virtual Earth</a> or <a href="http://www.wikimapia.org/maps?ll='+latitude+','+longitude+'&amp;spn=0.03,0.045&amp;t=k" class="external text">WikiMapia</a></li>\
</ul>';
			
			return true;
		}
	}
	return false;
}

// [[Category:Commons geocoding|{{PAGENAME}}]]