coordenadas de tela para latitude e longitude
-
08-07-2019 - |
Pergunta
Na biblioteca camadas aberta, a seguir é o método utilizado para converter uma ordenada tela co de latitude e longitude. Eu não sou capaz de descobrir a lógica encapsula este método?
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 );
}
Por favor alguém pode fornecer alguns ponteiros?
Solução
A função calcula a latitude e longitude para o ponto especificado com base na resolução atual do mapa, e a latitude e longitude do ponto central do mapa atual, ea distância que o ponto escolhido é a partir do centro do mapear.
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();
A pouco acima do código reúne as informações necessárias para o cálculo: O ponto central da visualização do mapa atual (que nos dará a latitude / longitude no ponto central), a resolução do mapa atual, e o tamanho atual mapa no a tela de utilizadores (que pode ser efectuada pelo tamanho do ecrã, etc).
Em seguida, o cálculo é o seguinte:
//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);
Em primeiro lugar tomar o co-ord x (em pixels) e subtraia a largura do mapa (em pixels). Isto dá-nos um novo co-ord x em que 0 é o pixel centro do mapa. delta-x deve ser agora um valor de pixel que varia entre - (size.w / 2) + a (size.w / 2). Em seguida, fazer o mesmo para co-ord y. Então delta-x e delta-y estão agora coordenadas cartesianas com origem no centro do mapa.
return new OpenLayers.LatLon( center.lat - delta_y * res, center.lon + delta_x * res );
Precisamos converter delta-x e delta-y de pixels para lat / lon. Primeiro vamos multiplicar delta-x e delta-y pela resolução atual. Isso nos dá a escala correta, mas não a origem correta. Adicionando centre.lat e centre.lon adusts para nos dar a latitude / longitude com base no mapa exibido no momento.
Finalmente a chamada 'nova OpenLayers.LatLon' apenas envolve os cálculos acima em um objeto Latlon, de modo que possa ser retornado da função como um objeto Latlon.
edit: ao trabalhar com pixels, um aumento x co-ord normalmente significa 'movimento certo', e um aumento em y co-ord normalmente significa 'mover-se'. Em um mapa, quando você aumenta Longitude, você normalmente 'mover para a direita'. No entanto Latitude está de cabeça para baixo; quando você aumenta Latitude, você normalmente 'descer' em um mapa.
obras Daí Latitude na direção oposta ao esquema normal de co-ord y em uma tela. Assim, no cálculo final um sinal de menos é usado para centre.lat mas uma vantagem para centre.lon.
Outras dicas
Eu reorganizados os comentários existentes, acrescentou um pouco mais e adicionado algum espaço em branco. Com sorte, você vai encontrar isso mais claro.
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 );
}
Tente isto:
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;
});