google-maps - 在 Google Static Maps API 中获取矩形的图像

标签 google-maps

我有两个 LatLong 点:矩形的左上点和右下点,我想使用 Google Maps API 检索此矩形的卫星图像。

我知道,由于缩放级别是一个整数,我可能无法获得这个精确的矩形,但是如果我获得更大的像素图就没有问题,只要我能够在 latlong 坐标中计算像素图的边界框。

由于像素图大小是固定的(比如 640x640)并且我有它的中心,如何找到合适的缩放级别并计算像素图的边界框?

最佳答案

在提供CalculateBoundsZoomLevel 函数的旧Google Maps API 博客中找到了一个python 引用链接。

源文件是here .

  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

关于google-maps - 在 Google Static Maps API 中获取矩形的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4267121/

相关文章:

android - 如何检测用户何时到达 Google map 中的目的地?我正在通过我的应用程序的 Intent 启动 Google map

reactjs - 在 react js中调整谷歌地图框架的大小

Android OnInfoWindowClickListener() 从未调用过

android - 缩放标记的图标并在 map 缩放时添加更多图标?

ios - 警告 : New version of Google Maps SDK for iOS available - How do I update?

java - Android Studio 上的 Google API 错误

android - 折线宽度转换 map api v2

android - 如何使用 Google Maps android API v2 获取我的位置更改事件?

jquery - 如何在 Bootstrap jQuery Tab 中重新加载 Google map

ios - 如何在 iOS Swift 中使用 GOOGLE MAPS 获取源和目的地之间的公交车站列表