我已经看到了类似的标题了一些问题,但他们似乎是指x
与y
像素坐标。
我问的实际片号x
和y
从谷歌地图getTile()
功能:
为了澄清这个问题...
给定getTile()函数中的x,y和zoom参数,如何找到图块的纬度和经度边界?
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()
。因此,如果可以得到边界,则可以使用边界内的任何纬度和经度点来找到最大的Zoom。
我想到的替代方法是创建图像并检查src url是否有错误(对我来说,这似乎是一个糟糕的主意,因为我会提出许多错误的要求,只是检查图像是否存在)。
var img = new Image;
img.onload = function() {/*imagery exists*/ }
img.onerror = function() {/*past maximum zoom*/ }
img.src = url;
编辑:
经过进一步调查,我意识到该getMaxZoomAtLatLng()
函数正在使用ajax调用,这不适合我的计划。但是我仍然对如何找到给定图块的纬度和经度边界感兴趣(这可能对其他应用程序很有用)。
假设使用墨卡托投影和256x256的tileSize的基本Google地图:
每个(x轴和y轴)上的瓦片数为Math.pow(2,zoom)
,因此在缩放时0
,地图使用1瓦片,在缩放1
4瓦片上,在缩放2
16瓦片上,依此类推。
首先计算瓷砖的西南/东北点。
磁贴的大小(以磅为单位)为 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
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句