簡體   English   中英

如何從谷歌地圖 xy 和縮放參數中獲取緯度和經度界限

[英]How to get Latitude and Longitude Bounds from Google Maps x y and zoom parameters

我見過一些標題相似的問題,但它們似乎指的是xy像素坐標。

我從 Google Maps getTile() function 詢問xy的實際圖塊數:

為了澄清這個問題......

給定 getTile() function 中的 x、y 和縮放參數,如何找到圖塊的緯度和經度邊界?

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   
}

目前我需要這個的唯一原因是我想確定這個平鋪投影的最大縮放級別。 從這個鏈接: 最大縮放,它指出為了找到最大縮放,我需要使用getMaxZoomAtLatLng()的緯度和經度值。 所以如果我能得到界限,那么我可以使用界限內的任何緯度和經度點來找到我的最大縮放。

我想到的替代方法是創建一個圖像並檢查 src url 是否有錯誤(這對我來說似乎是一個糟糕的主意,因為我會提出許多糟糕的請求來檢查圖像是否存在)。

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

編輯:

經過進一步調查,我意識到getMaxZoomAtLatLng() function 正在使用 ajax 調用,這不符合我的計划。 但我仍然對如何找到給定圖塊的緯度和經度邊界感興趣(這可能對其他應用程序有用)。

假設使用mercator-projection和tilexize為256x256的基本google-map:

每個(x軸和y軸)上的平鋪數量是Math.pow(2,zoom) ,因此在縮放0 ,地圖使用1個平鋪,放大1 4個平鋪,放大2個16平鋪等等。

首先計算瓷磚的西南/東北點。

圖塊的大小(以256/Math.pow(2,zoom) )為256/Math.pow(2,zoom)

西南點:

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

東北點:

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

這些點必須轉換為LatLngs。 使用地圖時,您可以使用地圖投影的fromLatLngToPoint方法。

有關自定義實施,請查看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/

我認為谷歌地圖平鋪系統類似於Bings地圖平鋪系統。 磁貼從右下角的左上角開始,每個磁貼為256x256: http//msdn.microsoft.com/en-us/library/bb259689.aspx

不確定這是否完全有助於邊界,但為了找到我去這里的瓷磚坐標的簡單顯示:

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

而到typescript就行了:

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

將緯度/經度更改為您想要的任何地方......他們有一個很好的用戶界面,可以計算縮放的所有變化。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM