简体   繁体   中英

How to reverse this mathematical conversion in android

I'm using this code to convert Google Map coordinates into an X,Y system on a custom overlay. This takes a latitude, longitude, and a zoom and converts it to x,y with a custom center point built in. What are the correct steps to have an x,y and convert it back to latitude and longitude.

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);
}

See if this is what you need:

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

and the other way around

mMap.getProjection().toScreenLocation(LatLng l)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM