스크린 공동은 위도와 경도를 의식합니다
-
08-07-2019 - |
문제
Open Layers 라이브러리에서 아래는 스크린 공동을 위도 및 경도로 변환하는 데 사용되는 방법입니다. 이 방법이 캡슐화되는 논리를 알아낼 수 없습니까?
getLatLonFromPoint: function (point) {
var center = this.getCenter();
//map center lat/lon
var res = this.getResolution();
//pre defined by the user. Represents the change in lat long per screen unit at the given zoom level
var size = this.getSize();
//this is the width and height of the div in which the map has to be displayed
var delta_x = point.x - (size.w / 2);
var delta_y = point.y - (size.h / 2);
return new OpenLayers.LatLon(
center.lat - delta_y * res,
center.lon + delta_x * res );
}
누군가 포인터를 제공 할 수 있습니까?
해결책
함수는 맵의 현재 해상도와 현재 맵의 중심 지점의 위도와 경도에 따라 지정된 지점의 위도와 경도를 계산하고, 선택한 지점이 맵의 중심에서 나온 거리입니다.
var center = this.getCenter(); //map center lat/lon var res = this.getResolution(); //pre defined by the user. Represents the change in lat long ... var size = this.getSize();
위의 코드는 계산에 필요한 정보를 수집합니다 : 현재 맵보기의 중심점 (중심점에서 LAT/LON을 제공 함), 현재 맵 해상도 및 사용자 화면의 현재 맵 크기 (화면 크기 등에 의해 영향을받을 수 있음).
그런 다음 계산은 다음과 같습니다.
//this is the width and height of the div in which the map has to be displayed var delta_x = point.x - (size.w / 2); var delta_y = point.y - (size.h / 2);
먼저 X 코드 (픽셀)를 가져 와서 맵의 너비 (픽셀)를 빼십시오. 이것은 0이 맵의 중심 픽셀 인 새로운 x 코드를 제공합니다. 델타 -X는 이제 -(size.w/2)에서 +(size.w/2) 범위의 픽셀 값이어야합니다. 그런 다음 Y 코드에 대해서도 똑같이합니다. 따라서 Delta-X와 Delta-Y는 이제지도의 중앙에 기원을 가진 직교 코르테시아어입니다.
return new OpenLayers.LatLon( center.lat - delta_y * res, center.lon + delta_x * res );
델타 -X와 델타 -y를 픽셀에서 LAT/LON으로 변환해야합니다. 먼저 우리는 델타 -X와 델타 -y에 현재 해상도에 곱합니다. 이것은 올바른 척도를 제공하지만 올바른 원점은 아닙니다. Centre.lat 및 Centre.lon Adusts는 현재 표시된지도를 기반으로 LAT/LON을 제공합니다.
마지막으로 'New OpenLayers.latlon'호출은 위의 계산을 라트 론 객체에 래핑하여 함수에서 Latlon 객체로 반환 할 수 있도록합니다.
편집 : 픽셀로 작업 할 때 X 코드의 증가는 일반적으로 '오른쪽으로 이동'을 의미하며 Y 코드의 증가는 일반적으로 '이동'을 의미합니다. 지도에서 경도를 높이면 보통 '바로 움직입니다'. 그러나 위도는 거꾸로되었습니다. 위도를 늘리면 일반적으로지도에서 '아래로 이동'합니다.
따라서 위도는 화면의 일반 Y 코드 체계와 반대 방향으로 작동합니다. 따라서 최종 계산에서 마이너스는 센터에 사용됩니다.
다른 팁
기존 주석을 재정렬하고 더 많은 공간을 추가했으며 공백을 추가했습니다. 바라건대, 당신은 이것을 더 명확하게 찾을 수 있기를 바랍니다.
getLatLonFromPoint: function (point) {
// point is the x and y screen coordinate
// map center lat/lon
var center = this.getCenter();
// pre defined by the user. Represents the change in lat long per screen unit at the given zoom level
var res = this.getResolution();
// this is the width and height of the screen (div) in which the map has to be displayed
var size = this.getSize();
// this is the distance of the point from the center of the screen (div)
var delta_x = point.x - (size.w / 2);
var delta_y = point.y - (size.h / 2);
// return the latitude and longitude
// these are calculated from the center lat/lon using the
// screen distances which are scaled (multiplied) by the resolution
return new OpenLayers.LatLon(
center.lat - delta_y * res,
center.lon + delta_x * res );
}
이 시도:
map.events.register("mousemove", map, function(e) {
var position = this.events.getMousePosition(e);
var p = map.getLonLatFromPixel(new OpenLayers.Pixel(position.x, position.y));
// your longitude value = p.lon;
// your latitude value = p.lat;
});