Google Maps Latitude and Longitude

Updated

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));
About these ads

15 Responses to “Google Maps Latitude and Longitude”


  1. 1 Roger 2008-02-12 at 09:03:38

    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.

  2. 3 ThomasV 2008-03-23 at 15:27:16

    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.

  3. 4 ThomasV 2008-03-23 at 15:32:21

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

  4. 5 ThomasV 2008-03-23 at 15:34:50

    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.

  5. 6 ThomasV 2008-03-23 at 15:40:41

    It is not OK :( , so change it manually after copy paste …

    It should be always character ‘
    as it is in first post!

  6. 7 Santhosh 2009-06-15 at 07:06:08

    Is there a way to find the location, if I know the coordinates?

  7. 8 Krishnamurthy 2010-11-10 at 11:39:12

    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.

  8. 11 zimmicz 2012-01-25 at 21:53:25

    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.

  9. 12 Simon Simple 2012-07-14 at 00:10:27

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


  1. 1 Address Book to Google Maps to RouteBuddy at bioneural.net Trackback on 2008-07-17 at 14:46:51
  2. 2 Getting longtitude and latitude coordinates from the centre of a google map « An Cuasán Trackback on 2009-08-20 at 20:15:06
  3. 3 Address Book to Google Maps to RouteBuddy | bioneural.net Trackback on 2011-09-17 at 17:56:49

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s





Follow

Get every new post delivered to your Inbox.

%d bloggers like this: