我有两个LatLong点:矩形的左上角和右下角点。我想使用Google Maps API检索该矩形的卫星图像。由于缩放级别是整数,因此可能无法获取完全匹配的矩形,但只要能够计算出纬度经度坐标系下该像素图的边界框,即使得到一个更大的像素图也没有问题。由于像素图大小固定(假设为640x640),因此如何找到适当的缩放级别并计算像素图的边界框?
在旧版Google Maps API博客中发现了一个与Python相关的参考资料,提供了函数CalculateBoundsZoomLevel。
源文件在这里。
def CalculateBoundsZoomLevel(self, bounds, view_size):
"""Given lat/lng bounds, returns map zoom level.
This method is used to take in a bounding box (southwest and northeast
bounds of the map view we want) and a map size and it will return us a zoom
level for our map. We use this because if we take the bottom left and
upper right on the map we want to show, and calculate what pixels they
would be on the map for a given zoom level, then we can see how many pixels
it will take to display the map at this zoom level. If our map size is
within this many pixels, then we have the right zoom level.
Args:
bounds: A list of length 2, each holding a list of length 2. It holds
the southwest and northeast lat/lng bounds of a map. It should look
like this: [[southwestLat, southwestLat], [northeastLat, northeastLng]]
view_size: A list containing the width/height in pixels of the map.
Returns:
An int zoom level.
"""
zmax = 18
zmin = 0
bottom_left = bounds[0]
top_right = bounds[1]
backwards_range = range(zmin, zmax)
backwards_range.reverse()
for z in backwards_range:
bottom_left_pixel = self.FromLatLngToPixel(bottom_left, z)
top_right_pixel = self.FromLatLngToPixel(top_right, z)
if bottom_left_pixel.x > top_right_pixel.x :
bottom_left_pixel.x -= self.CalcWrapWidth(z)
if abs(top_right_pixel.x - bottom_left_pixel.x) <= view_size[0] \
and abs(top_right_pixel.y - bottom_left_pixel.y) <= view_size[1] :
return z
return 0