[英]How to get Latitude and Longitude Bounds from Google Maps x y and zoom parameters
我见过一些标题相似的问题,但它们似乎指的是x
和y
像素坐标。
我从 Google Maps getTile()
function 询问x
和y
的实际图块数:
为了澄清这个问题......
给定 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
}
我认为谷歌地图平铺系统类似于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.