如何在android中反转这种数学转换

时间:2015-04-16 21:11:04

标签: android google-maps google-maps-android-api-2 coordinate-transformation

我使用此代码将Google地图坐标转换为自定义叠加层上的X,Y系统。这需要一个纬度,经度和缩放,并将其转换为x,y,内置自定义中心点。拥有x,y并将其转换回纬度和经度的正确步骤是什么。

static Double game_1_x = 1972.606;
static Double game_1_y = 3817.044;
static Double map_1_lng = 42.012002;
static Double map_1_lat = 42.850185;

static Double game_2_x = -1210.765;
static Double game_2_y = -3443.753;
static Double map_2_lng = -49.922088;
static Double map_2_lat = -83.293854;

public static String convertToXY(String lat, String lon, float zoom) {

    int mapSize = 0;

    if (zoom == 2.0f) {
        mapSize = 1024;
    } else if (zoom == 3.0f) {
        mapSize = 2048;
    } else if (zoom == 4.0f) {
        mapSize = 4096;
    } else if (zoom == 5.0f) {
        mapSize = 8192;
    }

    Double LAT = Double.valueOf(lat);
    Double LON = Double.valueOf(lon);

    // get marker x value
    Double markerLon = (LON + 180) * (mapSize / 360);

    // convert Lat to Radians
    Double markerLatRad = LAT * Math.PI / 180;

    // get marker y value
    Double mercN = Math.log(Math.tan((Math.PI / 4) + (markerLatRad / 2)));
    Double markerLat = (mapSize / 2) - (mapSize * mercN / (2 * Math.PI));

    // get map 1 x value
    Double m1lng = (map_1_lng + 180) * (mapSize / 360);
    // get map 2 x value
    Double m2lng = (map_2_lng + 180) * (mapSize / 360);

    // convert Lat to Radians
    Double m1LatRad = map_1_lat * Math.PI / 180;
    Double m2LatRad = map_2_lat * Math.PI / 180;

    // get map 1 y value
    Double mercNm1y = Math.log(Math.tan((Math.PI / 4) + (m1LatRad / 2)));
    Double m1lat = (mapSize / 2) - (mapSize * mercNm1y / (2 * Math.PI));
    // get map 2 y value
    Double mercNm2y = Math.log(Math.tan((Math.PI / 4) + (m2LatRad / 2)));
    Double m2lat = (mapSize / 2) - (mapSize * mercNm2y / (2 * Math.PI));


    Double X = game_1_x + (markerLon - m1lng) * (game_1_x - game_2_x) / (m1lng - m2lng);
    Double Y = game_1_y + (markerLat - m1lat) * (game_1_y - game_2_y) / (m1lat - m2lat);

    return String.valueOf(X) + "," + String.valueOf(Y);
}

1 个答案:

答案 0 :(得分:0)

看看你是否需要:

mMap = mMapView.getMap();
mMap.getProjection().fromScreenLocation(Point p)

反之亦然

mMap.getProjection().toScreenLocation(LatLng l)