Inspired by the post from Lifehacker, which is a re-post from tech-recipes, here’s how to get Google Maps coordinates in degrees, minutes and seconds:
javascript:coord = [gApplication.getMap().getCenter().lat(),gApplication.getMap().getCenter().lng()];
output = '';
for (x in [0,1]) {
neg = (coord[x] < 0);
if (neg) coord[x] *= -1;
deg = Math.floor(coord[x]);
minr = (coord[x] - deg) * 60;
min = Math.floor(minr);
sec = Math.floor((minr - min) * 60 * 100000)/100000;
output += (x==0?'Lat':', Long') + ': ' + deg + "° " + (min < 10?'0':'') + min + "' " + (sec < 10?'0':'') + sec + '" ' + (x==0?(neg?'S':'N'):(neg?'W':'E'));
};
void(prompt('', output));
Also do-able in one line
:
javascript:coord=[gApplication.getMap().getCenter().lat(),gApplication.getMap().getCenter().lng()];output='';for(x in [0,1]){neg=(coord[x]<0);if (neg) coord[x]*=-1;deg=Math.floor(coord[x]);minr=(coord[x]-deg)*60;min=Math.floor(minr);sec=Math.floor((minr - min)*60*100000)/100000;output+=(x==0?'Lat':', Long')+': '+deg+"° "+(min<10?'0':'')+min+"' "+(sec<10?'0':'')+sec+'" '+(x==0?(neg?'S':'N'):(neg?'W':'E'));};void(prompt('',output));
How to use? Just open up Google Maps, center the view to the place you want the coordinates of and paste the one-liner into the address bar. Or make a new bookmark, paste the line into “Location”, and “open” the bookmark while you’re in Google Maps.
Yay!
Of course, the question is, use +/- notation or north/south and east/west?
Le Update 1
Roger wanted the output to be pastable into Google Maps. Not hard actually…
Google Maps Compatible Output
javascript:coord=[gApplication.getMap().getCenter().lat(),gApplication.getMap().getCenter().lng()];output='';for(x in [0,1]){neg=(coord[x]<0);if (neg) coord[x]*=-1;deg=Math.floor(coord[x]);minr=(coord[x]-deg)*60;min=Math.floor(minr);sec=Math.floor((minr - min)*60*100000)/100000;output+=deg+"° "+(min<10?'0':'')+min+"' "+(sec<10?'0':'')+sec+'" '+(x==0?(neg?'S ':'N '):(neg?'W':'E'));};void(prompt('',output));
and ThomasV tried to comment but WordPress trying to be smart broke his quotes. I guess MapSource is a program?
MapSource Compatible Output
javascript:coord = [gApplication.getMap().getCenter().lat(),gApplication.getMap().getCenter().lng()]; output = ''; for (x in [0,1]) { neg = (coord[x] < 0); if (neg) coord[x] *= -1; output += (x==0?(neg?'S':'N'):(neg?'W':'E')) + coord[x] + ' '; }; void(prompt('', output));
Update 2009-02-08: Target Box
I updated the code, it now inserts a small semi-transparent box in the middle of the map, click on it and it will tell you the coordinates of the cent(re|er) of the map. Yay!
javascript:var tgt = document.createElement('div'); tgt.setAttribute('style', 'border: 1px solid blue; background-color: white; position: absolute; top: 49%; left: 49%; height: 2%; width: 2%; opacity: 0.5;'); tgt.onclick = function() { coord=[gApplication.getMap().getCenter().lat(),gApplication.getMap().getCenter().lng()];output='';for(x in [0,1]){neg=(coord[x]<0);if (neg) coord[x]*=-1;deg=Math.floor(coord[x]);minr=(coord[x]-deg)*60;min=Math.floor(minr);sec=Math.floor((minr - min)*60*100000)/100000;output+=(x==0?'Lat':', Long')+': '+deg+"° "+(min<10?'0':'')+min+"' "+(sec<10?'0':'')+sec+'" '+(x==0?(neg?'S':'N') : (neg?'W':'E'));};prompt('Coordinates',output); }; void(document.getElementById('map').appendChild(tgt));
Almost exactly what I wanted!
The ideal would be to have the output string from your one line in a format that could be pasted back into Google maps at some point in the future or by a third party. Such as 00 00 00N 0 00 00W
Thanks for your hard work.
Try this:
javascript:void(prompt(”,gApplication.getMap().getCenter()));
If you want to import in MapSource
oneline:
javascript:coord = [gApplication.getMap().getCenter().lat(),gApplication.getMap().getCenter().lng()]; output = ”; for (x in [0,1]) { neg = (coord[x] < 0); if (neg) coord[x] = -1*coord[x]; output += (x==0?(neg?’S':’N'):(neg?’W':’E')) + coord[x] + ‘ ‘; }; void(prompt(”, output));
ThomasV from Slovenia.
Again, some strnge copy paste… grrr, here we go again:
javascript:coord = [gApplication.getMap().getCenter().lat(),gApplication.getMap().getCenter().lng()]; output = ”; for (x in [0,1]) { neg = (coord[x] < 0); if (neg) coord[x] = -1*coord[x]; output += (x==0?(neg?’S':’N'):(neg?’W':’E')) + coord[x] + ‘ ‘; }; void(prompt(”, output));
Ok this web page convert ‘ ‘ to ” !
It should be
javascript:coord = [gApplication.getMap().getCenter().lat(),gApplication.getMap().getCenter().lng()]; output = ‘ ‘; for (x in [0,1]) { neg = (coord[x] < 0); if (neg) coord[x] = -1*coord[x]; output += (x==0?(neg?’S':’N'):(neg?’W':’E')) + coord[x] + ‘ ‘; }; void(prompt(‘ ‘, output));
Hope now it will be OK.
It is not OK
, so change it manually after copy paste …
It should be always character ‘
as it is in first post!
Is there a way to find the location, if I know the coordinates?
Hello,
I want information about how we can find the langitude and latitude, if i know the address. If you have a simple example send me.
javascript:void(prompt(”,gApplication.getMap().getCenter()));
genius
Hi,
how did you find out that the code had to start with gApplication? There is not a single word about something like this in the API documentation. Thanks for the answer.
Here is a simpler way to get jut the lat/long in a format that can be copied/pasted into googlemaps of other programs. format will look something like “(40.91351257612757, -99.66796875)”, without the quotes. Hope this helps
javascript:void(prompt(”,gApplication.getMap().getCenter()));