Getting longtitude and latitude coordinates from the centre of a google map

Googles long/lat is degrees only and a fractional component.

This script decomposed that into degrees, minutes and seconds. From there I could convert it from having seconds to having a fractional minute component, just like my GPS requires.

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

The output from this first stage was like the bellow.

Lat: 51° 40' 23.49798" N, Long: 8° 30' 06.43386" W

I wanted it to output degrees, minutes, and then a fractional component like the below, as was required by my Garmin mapping software (I was pulling location coordinates from google maps and inserting them as waypoints/route-vias in my Garmin app for uploading to my GPS device).

N51 40.39163302364358 W8 30.10723114013672

I tweaked that script to return same.

I created a bookmark on the Bookmark Toolbar of my browser, and set the code below as the target/URL.

To use it I simply click the bookmark button for it when looking at a google map, and the coordinates for the location at map centre are given in a pop-up box.

Here is my ammended version of script.

for(x in [0,1]){
//sec=Math.floor((minr - min)*60*100000)/100000;
min = minr;
output+=(x==0?(neg?'S':'N'):(neg?'W':'E'))+deg+" "+(min<10?'0':'')+min+" ";

Update: has the original and a few tweaks.


Leave a Reply

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

You are commenting using your 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 )

Google+ photo

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

Connecting to %s