1.经纬度转墨卡托

1.1Java:

    public static double[] lngLat2Mercator(double lng, double lat) {
        double[] xy = new double[2];
        double x = lng * 20037508.342789 / 180;
        double y = Math.log(Math.tan((90 + lat) * M_PI / 360)) / (M_PI / 180);
        y = y * 20037508.34789 / 180;
        xy[0] = x;
        xy[1] = y;
        return xy;
    }

1.2Scala:

/**
  * 坐标转换
  */
object CoorTransfom {
  private val M_PI = Math.PI

  //经纬度转墨卡托
  def lngLat2Mercator(lng: Double, lat: Double): Array[Double] = {
    val xy = new Array[Double](2)
    val x = lng * 20037508.342789 / 180
    var y = Math.log(Math.tan((90 + lat) * M_PI / 360)) / (M_PI / 180)
    y = y * 20037508.34789 / 180
    xy(0) = x
    xy(1) = y
    xy
  }

}
2.墨卡托转经纬度
    public static double[] mercator2LngLat(double mercatorX, double mercatorY) {
        double[] xy = new double[2];
        double x = mercatorX / 20037508.34 * 180;
        double y = mercatorY / 20037508.34 * 180;
        y = 180 / M_PI * (2 * Math.atan(Math.exp(y * M_PI / 180)) - M_PI / 2);
        xy[0] = x;
        xy[1] = y;
        return xy;
    }
3.proj4方式

原文地址:坐标系转换成墨卡托 java 实现,最好使用成熟proj4或proj470

Maven:

<dependency>
			<groupId>org.osgeo</groupId>
			<artifactId>proj4j</artifactId>
			<version>0.1.0</version>
		</dependency>

源码:

import org.osgeo.proj4j.CRSFactory;
import org.osgeo.proj4j.CoordinateReferenceSystem;
import org.osgeo.proj4j.CoordinateTransform;
import org.osgeo.proj4j.CoordinateTransformFactory;
import org.osgeo.proj4j.ProjCoordinate;

public class Test {

	static final String WGS84_PARAM = "+title=long/lat:WGS84 +proj=longlat +datum=WGS84 +units=degrees";
	CoordinateReferenceSystem WGS84 = crsFactory.createFromParameters("WGS84",
			WGS84_PARAM);

	private static final CoordinateTransformFactory ctFactory = new CoordinateTransformFactory();
	private static final CRSFactory crsFactory = new CRSFactory();

	private static CoordinateReferenceSystem createCRS(String crsSpec) {
		CoordinateReferenceSystem crs = null;
		// test if name is a PROJ4 spec
		if (crsSpec.indexOf("+") >= 0 || crsSpec.indexOf("=") >= 0) {
			crs = crsFactory.createFromParameters("Anon", crsSpec);
		} else {
			crs = crsFactory.createFromName(crsSpec);
		}
		// crs = crsFactory.createFromParameters("Anon", crsSpec);

		return crs;
	}

	public static void main(String[] args) {
		// new CoordinateTransformTester(true).checkTransform("EPSG:4269",
		// 117.19625, 31.83879,
		// "+proj=tmerc +lat_0=0 +lon_0=117 +y_0=0 +x_0=500000 +k=0.9996 +pm=0
		// +zone=50 +to_meter=1 +a=6378137 +rf=298.257223563 +nodefs",
		// 1640416.667, 916074.825, 0.1);


//		lonlat2m();
		m2lonlat();
	}

	private static void lonlat2m() {
		System.out.println("....");
		
		// 117.19625 31.83879 518568.9 3522583.9
		double x1 = 117.19625d;
		double y1 = 31.83879d;
		// double x2 = 518568.9d;
		// double y2 = 3522583.9d;

		// String srcCRS = "EPSG:4269";
		String WGS84_PARAM = "+title=long/lat:WGS84 +proj=longlat +datum=WGS84 +units=degrees";
		String tgtCRS = "+proj=tmerc +lat_0=0 +lon_0=117 +y_0=0 +x_0=500000 +k=0.9996 +zone=50 +to_meter=1 +a=6378137 +ellps=WGS84 +units=m +no_defs";

		CoordinateTransform trans = ctFactory
				.createTransform(createCRS(WGS84_PARAM), createCRS(tgtCRS));
		ProjCoordinate pout = new ProjCoordinate();

		ProjCoordinate p = new ProjCoordinate(x1, y1);

		trans.transform(p, pout);

		System.out.println(p.x);
		System.out.println(p.y);
		System.out.println(pout.x);
		System.out.println(pout.y);

		System.out.println("-------		// 117.19625 31.83879 518568.9 3522583.9 ");
		p = new ProjCoordinate(y1, x1);
		trans.transform(p, pout);

//		System.out.println(p.x);
//		System.out.println(p.y);
//		System.out.println(pout.x);
//		System.out.println(pout.y);
	}
	
	
	
	private static void m2lonlat() {
		System.out.println("....");
		
		// 117.19625 31.83879 518568.9 3522583.9
//		double x2 = 117.19625d;
//		double y2 = 31.83879d;
		 double x1 = 518568.9d;
		 double y1 = 3522583.9d;
		
		// String srcCRS = "EPSG:4269";
		String WGS84_PARAM = "+title=long/lat:WGS84 +proj=longlat +datum=WGS84 +units=degrees";
		String tgtCRS = "+proj=tmerc +lat_0=0 +lon_0=117 +y_0=0 +x_0=500000 +k=0.9996 +zone=50 +to_meter=1 +a=6378137 +ellps=WGS84 +units=m +no_defs";
		
		CoordinateTransform trans = ctFactory
				.createTransform( createCRS(tgtCRS),createCRS(WGS84_PARAM));
		ProjCoordinate pout = new ProjCoordinate();
		
		ProjCoordinate p = new ProjCoordinate(x1, y1);
		
		trans.transform(p, pout);
		
		System.out.println(p.x);
		System.out.println(p.y);
		System.out.println(pout.x);
		System.out.println(pout.y);
		
		System.out.println("-------		// 117.19625 31.83879 518568.9 3522583.9 ");
		p = new ProjCoordinate(y1, x1);
		trans.transform(p, pout);
		
//		System.out.println(p.x);
//		System.out.println(p.y);
//		System.out.println(pout.x);
//		System.out.println(pout.y);
	}
}