因为项目的关系,需要后台坐标转换,就想到了proj4库。下面是我在eclipse中使用的记录
导入jar
在pom.xml中加入
<!-- https://mvnrepository.com/artifact/org.locationtech.proj4j/proj4j -->
<dependency>
<groupId>org.locationtech.proj4j</groupId>
<artifactId>proj4j</artifactId>
<version>1.1.1</version>
</dependency>
开始使用
创建坐标投影对象
CRSFactory crsFactory = new CRSFactory();
String SourceCRS = "wgs84";
String SourceCRS_params="+proj=longlat +datum=WGS84 +no_defs ";
CoordinateReferenceSystem source = crsFactory.createFromParameters(SourceCRS, SourceCRS_params);
投影坐标的参数可以根据EPSG在http://epsg.io/中查找,这里不做解释
如果自定义地图的参数有变动,请看Proj.4坐标系统创建参数
坐标转换
import org.locationtech.proj4j.CRSFactory;
import org.locationtech.proj4j.CoordinateReferenceSystem;
import org.locationtech.proj4j.CoordinateTransform;
import org.locationtech.proj4j.CoordinateTransformFactory;
import org.locationtech.proj4j.ProjCoordinate;
public ProjCoordinate WGS84To2436(){
CRSFactory crsFactory = new CRSFactory();
//源坐标系统
String SourceCRS = "wgs84";
String SourceCRS_params="+proj=longlat +datum=WGS84 +no_defs ";
CoordinateReferenceSystem source = crsFactory.createFromParameters(SourceCRS, SourceCRS_params);
//目标坐标系统
String TargetCRS = "2436";
String TargetCRS_params="+proj=tmerc +lat_0=0 +lon_0=117 +k=1 +x_0=500000 +y_0=0 +ellps=krass +towgs84=15.8,-154.4,-82.3,0,0,0,0 +units=m +no_defs ";
CoordinateReferenceSystem target = crsFactory.createFromParameters(TargetCRS, get_CRS_paramStr(TargetCRS));
//定义转换类WGS84转2436
CoordinateTransformFactory ctf = new CoordinateTransformFactory();
CoordinateTransform transform = ctf.createTransform(source, target);
//WGS84坐标系转换
ProjCoordinate projCoordinate = new ProjCoordinate(x, y);
transform.transform(projCoordinate, projCoordinate);
//projCoordinate.x 和 projCoordinate.y 就是转换结果
return projCoordinate;
}
到此转换完成