如何获得纬度和经度界限从谷歌地图x y和缩放参数



我看到过一些类似标题的问题,但它们似乎是指xy像素坐标。

我正在询问谷歌地图getTile()功能中xy的实际瓷砖数量:

为了澄清问题…

给定getTile()函数中的x、y和缩放参数,如何找到tile的纬度和经度边界?

CoordMapType.prototype.getTile = function(coord, zoom, ownerDocument) {
    var x = coord.x,
    y = coord.y,
    url = "http://mt1.google.com/vt/lyrs=y&x="+x+"&y="+y+"&z="+zoom;
    //other stuff   
}

目前我需要这个的唯一原因是我想确定这个贴图投影的最大缩放级别。从这个链接:Maximum Zoom,它指出,为了找到最大变焦,我将需要使用getMaxZoomAtLatLng()的纬度和经度值。因此,如果我能得到边界,那么我就可以使用边界内的任何经纬度点来找到我的最大缩放

我想到的替代方案是创建一个图像并检查src url是否有错误(这对我来说似乎是一个糟糕的想法,因为我会做出许多糟糕的请求,只是为了检查图像是否存在)。

var img = new Image;
img.onload = function() {/*imagery exists*/ }
img.onerror = function() {/*past maximum zoom*/ }
img.src = url;
编辑:

经过进一步调查,我意识到getMaxZoomAtLatLng()函数正在使用ajax调用,这将不适合我的计划。但是我仍然对如何找到给定瓷砖的纬度和经度边界感兴趣(这可能对其他应用程序很有用)。

假设使用墨卡托投影的基本谷歌地图,tile大小为256x256:

每个(x轴和y轴)上的瓷砖数量是Math.pow(2,zoom),因此缩放0时地图使用1个瓷砖,缩放1时使用4个瓷砖,缩放2时使用16个瓷砖,依此类推。

首先计算贴图的西南/东北方向。

贴图的大小(点数)为256/Math.pow(2,zoom)

southWest-point:

x = tile.x * tileSizeInPoints
y = (tile.y * tileSizeInPoints) + tileSizeInPoints

northEast-point:

x = (tile.x * tileSizeInPoints) + tileSizeInPoints
y = tile.y * tileSizeInPoints

这些点必须翻译成拉丁文。当你使用地图时,你可以使用地图投影的fromlatlingtoppoint方法。

关于自定义实现,请查看https://developers.google.com/maps/documentation/javascript/examples/map-coordinates。

一个可能与api无关的实现:

MERCATOR={
  fromLatLngToPoint:function(latLng){
     var siny =  Math.min(Math.max(Math.sin(latLng.lat* (Math.PI / 180)), 
                                   -.9999),
                          .9999);
     return {
      x: 128 + latLng.lng * (256/360),
      y: 128 + 0.5 * Math.log((1 + siny) / (1 - siny)) * -(256 / (2 * Math.PI))
     };
  },
  fromPointToLatLng: function(point){
     return {
      lat: (2 * Math.atan(Math.exp((point.y - 128) / -(256 / (2 * Math.PI)))) -
             Math.PI / 2)/ (Math.PI / 180),
      lng:  (point.x - 128) / (256 / 360)
     };
  },
  getTileAtLatLng:function(latLng,zoom){
    var t=Math.pow(2,zoom),
        s=256/t,
        p=this.fromLatLngToPoint(latLng);
        return {x:Math.floor(p.x/s),y:Math.floor(p.y/s),z:zoom};
  },
  getTileBounds:function(tile){
    tile=this.normalizeTile(tile);
    var t=Math.pow(2,tile.z),
        s=256/t,
        sw={x:tile.x*s,
            y:(tile.y*s)+s},
        ne={x:tile.x*s+s,
            y:(tile.y*s)};
        return{sw:this.fromPointToLatLng(sw),
               ne:this.fromPointToLatLng(ne)
              }
  },
  normalizeTile:function(tile){
    var t=Math.pow(2,tile.z);
    tile.x=((tile.x%t)+t)%t;
    tile.y=((tile.y%t)+t)%t;
    return tile;
  }
}

调用MERCATOR.getTileBounds(),提供一个对象作为参数,格式如下:

{
 x:tileIndexX,
 y:tileIndexY,
 z:zoom 
}

演示: http://jsfiddle.net/doktormolle/55Nke/

不确定这是否完全有助于边界,但为了找到一个简单的瓷砖坐标显示,我在这里:

https://developers.google.com/maps/documentation/javascript/examples/map-coordinates

和打字的行:

  const chicago = new google.maps.LatLng(-33.76781028848151, 150.73644505329204
);

把lat/long改成你想要的…他们有一个很好的UI可以计算缩放的所有变化

我认为Google地图平铺系统和bing地图平铺系统很相似。瓷砖从左上角开始到右下角,每个瓷砖是256x256:http://msdn.microsoft.com/en-us/library/bb259689.aspx.

最新更新