Searching for some sample code for converting a point in WGS84 coordinate system to a map position in Google Maps (pixel position), also supporting zoom levels.

If the codes is well commented, then it can also be in some other language.

You can also point me to a open source Java project :)

Some resources found:

OpenLayer implementation.

JOSM project

Excellent Java Map Projection Library from JH LABS. This is a pure java PROJ.4 port. Does projection from WGS84 to meters. From there it's quite straightforward to convert meters to tile pixels.


Someone took the javascript code from Google Maps and ported it to python:

I've used this and it works great.

Jon Works
+3  A: 

GeoTools has code to transform to and from about any coordinate system you could imagine, and among them also Google Map's. It's also open source. However, it should also be pointed out that GeoTools is a large library, so if you're looking something small, quick and easy, it's likely not the way to go.

I would highly recommend it though if you're going to do other GIS/coordinate transformations, etc. as well.

If you use GeoTools or something similar, you might also be interested in knowing that the Google Map coordinate system is called EPSG 3785.

+1  A: 

I recently spent a lot of tie working with learning about various map datums and conversions. Check out this guys' project -- it shows the USNG coordinates in a Google map. USNG uses WGS 84.

The javascript is GPL'd. If you search around, I think you can find it in C, but I am not aware of a public Java version. A previous version of the code is actually also used in the National Map Viewer, which is public domain.

+3  A: 

Tile utility code in Java on (great resource for google map developers)


Here are the functions in JavaSCript ... As extracted from OpenLayers

function toMercator (lon, lat) {
  var x = lon * 20037508.34 / 180;
  var y = Math.log(Math.tan((90 + lat) * Math.PI / 360)) / (Math.PI / 180);
  y = y * 20037508.34 / 180;

  return [x, y];

function inverseMercator (x, y) {
  var lon = (x / 20037508.34) * 180;
  var lat = (y / 20037508.34) * 180;

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

  return [lon, lat];

Fairly straightforward to convert to Java