【发布时间】:2015-04-17 06:26:16
【问题描述】:
我正在使用此代码将 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);
}
【问题讨论】:
-
我不打算做数学,但是让触摸通过您的叠加层并从地图中获取纬度和经度。
-
我希望能够让用户使用 x,y 输入自定义标记并检索 lat lng
标签: android google-maps google-maps-android-api-2 coordinate-transformation