我有一个存储在二维数组中的谷歌静态地图。当用户点击一个像素时,我知道行/列 [0 - 1024]。
我需要将该行 col 转换回 lat lng。我一直在尝试使用下面的代码来进行从像素到纬度 lng 和返回的转换。
我所做的是获取中心 lat lng 和缩放级别。
- 使用 fromLatLngToPoint 将中心 lat lng 转换为像素。
- 使用图像中的行/列和图像的宽度和高度得到
private static PointF getPointFromRowCol(int row, int col, int width, int height, PointF center) {
double adjustedStartCol = center.x - ((double)width / 2);
double adjustedStartRow = center.y - ((double)height / 2);
double adjustedCol = adjustedStartCol + col;
double adjustedRow = adjustedStartRow + row;
PointF adjustedWorldPoint = new PointF(adjustedCol, adjustedRow);
return GoogleMapsProjection2.fromPointToLatLng(adjustedWorldPoint, 17);
}
问题是当我将生成的 lat lng 放回谷歌地图时,它偏离了 100 米。
有任何想法吗?
public final class GoogleMapsProjection2 {
private final int TILE_SIZE = 256;
private PointF _pixelOrigin;
public double _pixelsPerLonDegree;
public double _pixelsPerLonRadian;
public GoogleMapsProjection2() {
this._pixelOrigin = new PointF(TILE_SIZE / 2.0, TILE_SIZE / 2.0);
this._pixelsPerLonDegree = TILE_SIZE / 360.0;
this._pixelsPerLonRadian = TILE_SIZE / (2 * Math.PI);
}
double bound(double val, double valMin, double valMax) {
double res;
res = Math.max(val, valMin);
res = Math.min(val, valMax);
return res;
}
double degreesToRadians(double deg) {
return deg * (Math.PI / 180);
}
double radiansToDegrees(double rad) {
return rad / (Math.PI / 180);
}
public PointF fromLatLngToPoint(double lat, double lng, int zoom) {
PointF point = new PointF(0, 0);
point.x = _pixelOrigin.x + lng * _pixelsPerLonDegree;
// Truncating to 0.9999 effectively limits latitude to 89.189. This is
// about a third of a tile past the edge of the world tile.
double siny = bound(Math.sin(degreesToRadians(lat)), -0.9999, 0.9999);
point.y = _pixelOrigin.y + 0.5 * Math.log((1 + siny) / (1 - siny)) * -_pixelsPerLonRadian;
int numTiles = 1 << zoom;
point.x = point.x * numTiles;
point.y = point.y * numTiles;
return point;
}
public PointF fromPointToLatLng(PointF point, int zoom) {
int numTiles = 1 << zoom;
point.x = point.x / numTiles;
point.y = point.y / numTiles;
double lng = (point.x - _pixelOrigin.x) / _pixelsPerLonDegree;
double latRadians = (point.y - _pixelOrigin.y) / -_pixelsPerLonRadian;
double lat = radiansToDegrees(2 * Math.atan(Math.exp(latRadians)) - Math.PI / 2);
return new PointF(lat, lng);
}
public static PointF fromWorldToPixel(PointF world){
PointF pixelPoint = new PointF(world.x * (1 << 17), world.y * (1 << 17));
return pixelPoint;
}
public static PointF pixelToWorld(PointF world){
PointF pixelPoint = new PointF(world.x / (1 << 17), world.y / (1 << 17));
return pixelPoint;
}
}