GPS coordinates of tile corners

530 views
Skip to first unread message

ad...@svk-zw.de

unread,
Oct 6, 2010, 4:57:23 AM10/6/10
to Google Maps JavaScript API v3
Hey there,

I' ve searched the hole web, but I think I am not good enough in most
script languages.
What I need is a program where I can entere the center-coordinates of
the google map tile, and the wanted zoom level. The program has to
calculate the gps coordinates of 2 corners (bsp. Upper-left, lower-
right) and give them back.
Example [ullong, ullat, lrlong, lrlat] = gpscorners(long, lat,
zoomelevel)

I've found this one here, but I don't know how to use it.

import java.applet.*;
import java.awt.*;
import java.lang.*;

public class Googlecoordinates

{


/**
* returns a Rectangle2D with x = lon degrees , y = lat degrees,
* width=lonSpan, height=latSpan
* for an x,y,zoom as used by google.
*/
public static Rectangle2D.Double getLatLong(int x, int y, int
zoom)
{
double lon = -180; // x
double lonWidth = 360; // width 360


//double lat = -90; // y
//double latHeight = 180; // height 180
double lat = -1;
double latHeight = 2;


int tilesAtThisZoom = 1 << (17 - zoom);
lonWidth = 360.0 / tilesAtThisZoom;
lon = -180 + (x * lonWidth);
latHeight = -2.0 / tilesAtThisZoom;
lat = 1 + (y * latHeight);


// convert lat and latHeight to degrees in a mercator
projection
// note that in fact the coordinates go from
// about -85 to +85 not -90 to 90!
latHeight += lat;
latHeight = (2 * Math.atan(Math.exp(Math.PI * latHeight))) -
(Math.PI / 2);
latHeight *= (180 / Math.PI);


lat = (2 * Math.atan(Math.exp(Math.PI * lat))) - (Math.PI / 2);
lat *= (180 / Math.PI);


latHeight -= lat;


if (lonWidth < 0) {
lon = lon + lonWidth;
lonWidth = -lonWidth;
}


if (latHeight < 0) {
lat = lat + latHeight;
latHeight = -latHeight;
}


return new Rectangle2D.Double(lon, lat, lonWidth, latHeight);
}

}


Hope you can help me. Thanks for your time!

Nathan Raley

unread,
Oct 6, 2010, 9:54:22 AM10/6/10
to google-map...@googlegroups.com
You know the size of your container.  You know the center lat.  You can convert the center lat to pixel, adjust to top-left and bottom-right respectively, then convert from pixel to coordinates.


--
You received this message because you are subscribed to the Google Groups "Google Maps JavaScript API v3" group.
To post to this group, send email to google-map...@googlegroups.com.
To unsubscribe from this group, send email to google-maps-js-a...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-maps-js-api-v3?hl=en.


ad...@svk-zw.de

unread,
Oct 11, 2010, 2:44:01 AM10/11/10
to Google Maps JavaScript API v3
Hi,

thanks for your answer,
I tried a little and I can find the corner coordiantes only for
longitude,.

but @ latitude I don't know the lat length of my tile.... so how can i
get those?

again, thanks for your help.
> > google-maps-js-a...@googlegroups.com<google-maps-js-api-v3%2B­unsub...@googlegroups.com>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/google-maps-js-api-v3?hl=en.- Zitierten Text ausblenden -
>
> - Zitierten Text anzeigen -

Nathan Raley

unread,
Oct 11, 2010, 8:58:30 AM10/11/10
to google-map...@googlegroups.com
Every tile is 256x256.  Convert your center lat,lon to pixels and then adjust accordingly to get the lat,lon of your corners.  Remember 0,0 is top left most tile coordiantes.

To unsubscribe from this group, send email to google-maps-js-a...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages