WGS84 到 Google 地图位置和返回的 Java 代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 07:08:55  来源:igfitidea点击:

Java code for WGS84 to Google map position and back

javagoogle-mapsmappingwgs84

提问by JaanusSiim

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.

搜索一些示例代码,用于将 WGS84 坐标系中的点转换为 Google 地图中的地图位置(像素位置),也支持缩放级别。

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

您还可以将我指向一个开源 Java 项目:)

Some resources found:

找到了一些资源:

OpenLayerimplementation.

OpenLayer实现。

JOSMproject

JOSM项目

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.

来自 JH LABS 的优秀Java 地图投影库。这是一个纯java PROJ.4 端口。从 WGS84 投影到米。从那里将米转换为平铺像素非常简单。

采纳答案by andyuk

Tile utility code in Javaon mapki.com (great resource for google map developers)

mapki.com上的 Java 平铺实用程序代码(谷歌地图开发人员的绝佳资源)

回答by Jonathan Works

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

有人把谷歌地图上的javascript代码移植到python上:gmerc.py

I've used this and it works great.

我用过这个,效果很好。

回答by Liedman

GeoToolshas 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 largelibrary, so if you're looking something small, quick and easy, it's likely not the way to go.

GeoTools有代码来转换你能想象到的任何坐标系,其中还有谷歌地图。它也是开源的。但是,还应该指出的是,GeoTools 是一个大型库,因此如果您正在寻找小型、快速和简单的东西,它可能不是您要走的路。

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

不过,如果您还要进行其他 GIS/坐标转换等,我会强烈推荐它。

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

如果您使用 GeoTools 或类似工具,您可能还想知道 Google 地图坐标系称为 EPSG 3785。

回答by Ro.

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

以下是 JavaSCRipt 中的函数...摘自 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

转换为 Java 相当简单

回答by biphobe

I ported this to PHP - here's the code, if anyone would need it:

我将它移植到 PHP - 这是代码,如果有人需要的话:

To mercator:

对墨卡托:

$lon = ($lon * 20037508.34) / 180;
$lat = log(tan((90 + $lat) * M_PI / 360)) / (M_PI / 180);
$lat = $lat * 20037508.34 / 180;

From mercator:

从墨卡托:

$lon = ($lon / 20037508.34) * 180;
$lat = ($lat / 20037508.34) * 180;
$lat = 180/M_PI * (2 * atan(exp($lat * M_PI / 180)) - M_PI / 2);

回答by Sandeep

/*
 * Utility functions to transform between wgs84 and google projection coordinates
 * Derived from openmap http://openmap.bbn.com/
 */

public class MercatorTransform {
    public final static double NORTH_POLE = 90.0;
    public final static double SOUTH_POLE = -NORTH_POLE;
    public final static double DATELINE = 180.0;
    public final static double LON_RANGE = 360.0;

    final public static transient double wgs84_earthEquatorialRadiusMeters_D = 6378137.0;
    private static double latfac = wgs84_earthEquatorialRadiusMeters_D;
    private static double lonfac = wgs84_earthEquatorialRadiusMeters_D;

    final public static transient double HALF_PI_D = Math.PI / 2.0d;

    /**
     * Returns google projection coordinates from wgs84 lat,long coordinates
     */
    public static double[] forward(double lat, double lon) {

        lat = normalizeLatitude(lat);
        lon = wrapLongitude(lon);

        double latrad = Math.toRadians(lat);
        double lonrad = Math.toRadians(lon);

        double lat_m = latfac * Math.log(Math.tan(((latrad + HALF_PI_D) / 2d)));
        double lon_m = lonfac * lonrad;

        double[] x = { lon_m, lat_m };
        return x;
    }

    /**
     * Returns wgs84 lat,long coordinates from google projection coordinates
     */
    public static float[] inverse(float lon_m, float lat_m) {
        double latrad = (2d * Math.atan(Math.exp(lat_m / latfac))) - HALF_PI_D;
        double lonrad = lon_m / lonfac;

        double lat = Math.toDegrees(latrad);
        double lon = Math.toDegrees(lonrad);

        lat = normalizeLatitude(lat);
        lon = wrapLongitude(lon);
        float[] x = { (float) lat, (float) lon };

        return x;
    }

    private static double wrapLongitude(double lon) {
        if ((lon < -DATELINE) || (lon > DATELINE)) {
            lon += DATELINE;
            lon = lon % LON_RANGE;
            lon = (lon < 0) ? DATELINE + lon : -DATELINE + lon;
        }
        return lon;
    }

    private static double normalizeLatitude(double lat) {
        if (lat > NORTH_POLE) {
            lat = NORTH_POLE;
        }
        if (lat < SOUTH_POLE) {
            lat = SOUTH_POLE;
        }
        return lat;
    }

}