<p>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. </p>

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

<p>You can also point me to a open source Java project :)</p>

<p>Some resources found:</p>

<p><a href="http://trac.openlayers.org/browser/trunk/openlayers/lib/OpenLayers/Layer" rel="nofollow noreferrer">OpenLayer</a> implementation. </p>

<p><a href="http://josm.openstreetmap.de/browser/trunk/src/org/openstreetmap/josm/data/projection" rel="nofollow noreferrer">JOSM</a> project</p>

<p>Excellent <a href="https://github.com/OSUCartography/JMapProjLib" rel="nofollow noreferrer">Java Map Projection Library </a> 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.</p>

[{'answer_id': 7668, 'author': 'Jonathan Works', 'author_id': 309844, 'author_profile': 'https://Stackoverflow.com/users/309844', 'pm_score': 0, 'selected': False, 'text': '<p>Someone took the javascript code from Google Maps and ported it to python: <a href="http://code.google.com/p/gheat/source/browse/trunk/__/lib/python/gmerc.py" rel="nofollow noreferrer">gmerc.py</a></p>\n\n<p>I\'ve used this and it works great.</p>\n'}, {'answer_id': 7679, 'author': 'Liedman', 'author_id': 890, 'author_profile': 'https://Stackoverflow.com/users/890', 'pm_score': 2, 'selected': False, 'text': '<p><a href="http://web.archive.org/web/20090810085708/http://geotools.codehaus.org:80/" rel="nofollow noreferrer">GeoTools</a> 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 <strong>large</strong> library, so if you\'re looking something small, quick and easy, it\'s likely not the way to go.</p>\n<p>I would highly recommend it though if you\'re going to do other GIS/coordinate transformations, etc. as well.</p>\n<p>If you use GeoTools or something similar, you might also be interested in knowing that the Google Map coordinate system is called EPSG 3785.</p>\n'}, {'answer_id': 32551, 'author': 'andyuk', 'author_id': 2108, 'author_profile': 'https://Stackoverflow.com/users/2108', 'pm_score': 4, 'selected': True, 'text': '<p><a href="http://web.archive.org/web/20110809084551/http://mapki.com/wiki/Tile_utility_code_in_Java" rel="nofollow noreferrer">Tile utility code in Java</a> on mapki.com (great resource for google map developers)</p>\n'}, {'answer_id': 2373697, 'author': 'Ro.', 'author_id': 103446, 'author_profile': 'https://Stackoverflow.com/users/103446', 'pm_score': 2, 'selected': False, 'text': '<p>Here are the functions in JavaSCript ... As extracted from OpenLayers </p>\n\n<pre><code>function toMercator (lon, lat) {\n  var x = lon * 20037508.34 / 180;\n  var y = Math.log(Math.tan((90 + lat) * Math.PI / 360)) / (Math.PI / 180);\n  y = y * 20037508.34 / 180;\n\n  return [x, y];\n  }\n\nfunction inverseMercator (x, y) {\n  var lon = (x / 20037508.34) * 180;\n  var lat = (y / 20037508.34) * 180;\n\n  lat = 180/Math.PI * (2 * Math.atan(Math.exp(lat * Math.PI / 180)) - Math.PI / 2);\n\n  return [lon, lat];\n  }\n</code></pre>\n\n<p>Fairly straightforward to convert to Java</p>\n'}, {'answer_id': 8051786, 'author': 'biphobe', 'author_id': 889665, 'author_profile': 'https://Stackoverflow.com/users/889665', 'pm_score': 2, 'selected': False, 'text': "<p>I ported this to PHP - here's the code, if anyone would need it:</p>\n\n<p>To mercator:</p>\n\n<pre><code>$lon = ($lon * 20037508.34) / 180;\n$lat = log(tan((90 + $lat) * M_PI / 360)) / (M_PI / 180);\n$lat = $lat * 20037508.34 / 180;\n</code></pre>\n\n<p>From mercator:</p>\n\n<pre><code>$lon = ($lon / 20037508.34) * 180;\n$lat = ($lat / 20037508.34) * 180;\n$lat = 180/M_PI * (2 * atan(exp($lat * M_PI / 180)) - M_PI / 2);\n</code></pre>\n"}, {'answer_id': 11747267, 'author': 'Sandeep', 'author_id': 218857, 'author_profile': 'https://Stackoverflow.com/users/218857', 'pm_score': 1, 'selected': False, 'text': '<pre><code>/*\n * Utility functions to transform between wgs84 and google projection coordinates\n * Derived from openmap http://openmap.bbn.com/\n */\n\npublic class MercatorTransform {\n    public final static double NORTH_POLE = 90.0;\n    public final static double SOUTH_POLE = -NORTH_POLE;\n    public final static double DATELINE = 180.0;\n    public final static double LON_RANGE = 360.0;\n\n    final public static transient double wgs84_earthEquatorialRadiusMeters_D = 6378137.0;\n    private static double latfac = wgs84_earthEquatorialRadiusMeters_D;\n    private static double lonfac = wgs84_earthEquatorialRadiusMeters_D;\n\n    final public static transient double HALF_PI_D = Math.PI / 2.0d;\n\n    /**\n     * Returns google projection coordinates from wgs84 lat,long coordinates\n     */\n    public static double[] forward(double lat, double lon) {\n\n        lat = normalizeLatitude(lat);\n        lon = wrapLongitude(lon);\n\n        double latrad = Math.toRadians(lat);\n        double lonrad = Math.toRadians(lon);\n\n        double lat_m = latfac * Math.log(Math.tan(((latrad + HALF_PI_D) / 2d)));\n        double lon_m = lonfac * lonrad;\n\n        double[] x = { lon_m, lat_m };\n        return x;\n    }\n\n    /**\n     * Returns wgs84 lat,long coordinates from google projection coordinates\n     */\n    public static float[] inverse(float lon_m, float lat_m) {\n        double latrad = (2d * Math.atan(Math.exp(lat_m / latfac))) - HALF_PI_D;\n        double lonrad = lon_m / lonfac;\n\n        double lat = Math.toDegrees(latrad);\n        double lon = Math.toDegrees(lonrad);\n\n        lat = normalizeLatitude(lat);\n        lon = wrapLongitude(lon);\n        float[] x = { (float) lat, (float) lon };\n\n        return x;\n    }\n\n    private static double wrapLongitude(double lon) {\n        if ((lon &lt; -DATELINE) || (lon &gt; DATELINE)) {\n            lon += DATELINE;\n            lon = lon % LON_RANGE;\n            lon = (lon &lt; 0) ? DATELINE + lon : -DATELINE + lon;\n        }\n        return lon;\n    }\n\n    private static double normalizeLatitude(double lat) {\n        if (lat &gt; NORTH_POLE) {\n            lat = NORTH_POLE;\n        }\n        if (lat &lt; SOUTH_POLE) {\n            lat = SOUTH_POLE;\n        }\n        return lat;\n    }\n\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/7661', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/706/']