在google.maps.Point中转换(x,y)像素坐标 [英] Convert (x, y) pixel coordinates in google.maps.Point

查看:293
本文介绍了在google.maps.Point中转换(x,y)像素坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图根据我的x,y像素坐标(当然还有地图选项,例如缩放和居中)找出LatLng.

I am trying to find out the LatLng based on my x,y pixel coordinates (and of course map options, such as zoom and center).

为此,我发布了另一个帖子提出了这个解决方案:

In order to do so, I posted another question and someone came up with this solution, from this post:

/**
* @param {google.maps.Map} map
* @param {google.maps.Point} point
* @param {int} z
* @return {google.maps.LatLng}
*/
var pointToLatlng = function(map, point, z){
    var scale = Math.pow(2, z);
    var normalizedPoint = new google.maps.Point(point.x / scale, point.y / scale);
    var latlng = map.getProjection().fromPointToLatLng(normalizedPoint);
    return latlng; 
};

从代码示例中您可以注意到,该函数使用google.maps.Point作为参数,因此我需要将屏幕像素坐标转换为google.maps.Point,而且我不知道如何操作,因为它们的文档API的详细信息不是很详细...

As you can notice from the code sample, the function uses as argument a google.maps.Point, therefore I need to convert my screen pixel coordinate into a google.maps.Point and I have no clue how, since their documentation of the API is not quite verbose...

你能帮我吗?还是我在途中错过了什么?

Can you please help me? Or am I missing something on the way?

推荐答案

经过研究和失败,我提出了一个解决方案. 根据此链接中的文档,我发现Google积分是计算得出的在x:[0-256],y:[0-256](图块为256x256像素)的范围内,而(0,0)点是地图的最左点(请查看链接以获取更多信息).

After some research and some fails I came up with a solution. Following the documentation from this link I found out that the google Points are computed in the range of x:[0-256], y:[0-256] (a tile being 256x256 pixels) and the (0,0) point being the leftmost point of the map (check the link for more information).

但是,我的方法如下:

  • 具有x和y坐标(它们是屏幕上的坐标-在地图上),我计算了响应于包含地图的div放置x和y坐标的百分比(在我的情况下,孔窗口)

  • having the x and y coordinates (which are coordinates on the screen - on the map) I computed the percentage where the x and y coordinates were placed in response to the div containing the map (in my case, the hole window)

计算(可见)地图的NortEast和SouthWest LatLng边界

computed the NortEast and SouthWest LatLng bounds of the (visible) map

转换了Google积分中的范围

converted the bounds in google Points

借助x和y的边界和百分比在google points中计算了新的lat和lng

computed the new lat and lng, in google points, with the help of the boundaries and percentage of x and y

  // retrieve the lat lng for the far extremities of the (visible) map
  var latLngBounds = map.getBounds();
  var neBound = latLngBounds.getNorthEast();
  var swBound = latLngBounds.getSouthWest();

  // convert the bounds in pixels
  var neBoundInPx = map.getProjection().fromLatLngToPoint(neBound);
  var swBoundInPx = map.getProjection().fromLatLngToPoint(swBound);

  // compute the percent of x and y coordinates related to the div containing the map; in my case the screen
  var procX = x/window.innerWidth;
  var procY = y/window.innerHeight;

  // compute new coordinates in pixels for lat and lng;
  // for lng : subtract from the right edge of the container the left edge, 
  // multiply it by the percentage where the x coordinate was on the screen
  // related to the container in which the map is placed and add back the left boundary
  // you should now have the Lng coordinate in pixels
  // do the same for lat
  var newLngInPx = (neBoundInPx.x - swBoundInPx.x) * procX + swBoundInPx.x;
  var newLatInPx = (swBoundInPx.y - neBoundInPx.y) * procY + neBoundInPx.y;
  var finalResult = new google.maps.Point(newLngInPx, newLatInPx);

这篇关于在google.maps.Point中转换(x,y)像素坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆