需要对地图进行标点,参考了百科上的经纬度转xy轴,但考虑到更多情况下需要的是百分比.因此进行了修改
public class LLTransformXY {
private double startLongitude;
private double endLongitude;
private double startLatitude;
private double endLatitude;
/**
* @param startLongitude 起始的经度
* @param endLongitude 结束的维度
* @param startLatitude 起始的维度
* @param endLatitude 结束的维度
*/
public LLTransformXY(double startLongitude, double endLongitude, double startLatitude, double endLatitude) {
this.startLongitude = startLongitude;
this.endLongitude = endLongitude;
this.startLatitude = startLatitude;
this.endLatitude = endLatitude;
}
/**
* 投影的算法(墨卡托投影)
* @param longitude 需要转换的经线
* @param latitude 需要转化的纬线
* @return 百分比 x(0 - 1) y(0-1)
*/
public double[] transform(double longitude, double latitude){
double percentX = (longitude - startLatitude)/(endLongitude -startLongitude);
double y = Math.signum(latitude) * Math.log(Math.tan(Math.toRadians(45 + Math.abs(latitude/2))));
double startY = Math.signum(startLatitude) * Math.log(Math.tan(Math.toRadians(45 + Math.abs(startLatitude/2))));
double endY = Math.signum(endLongitude) * Math.log(Math.tan(Math.toRadians(45 + Math.abs(endLatitude/2))));
double percentY = (y-startY)/(endY - startY);
return new double[]{percentX,percentY};
}
}