【问题标题】:Radius of "viewable" region in google maps v3谷歌地图 v3 中“可见”区域的半径
【发布时间】:2011-04-01 08:08:45
【问题描述】:

我需要知道如何在 google maps API v3 中检索可视缩放级别的半径。

例如,如果我的缩放级别为 3,并且取决于用户的屏幕尺寸(假设为 400x400 可视区域),我如何获得可视区域的“半径”圆。

另外,我目前正在使用 map.fitBounds() 来处理我添加到地图中的所有点,所以我真正需要 是所有边界的半径。我想要的是可以输入数据库应用程序的“20 英里”之类的内容。

【问题讨论】:

    标签: google-maps google-maps-api-3


    【解决方案1】:

    半径将等于从边界中心到边界角之一的距离。使用the calculations from this page 的大圆距离公式,我得出以下结论:

    var bounds = map.getBounds();
    
    var center = bounds.getCenter();
    var ne = bounds.getNorthEast();
    
    // r = radius of the earth in statute miles
    var r = 3963.0;  
    
    // Convert lat or lng from decimal degrees into radians (divide by 57.2958)
    var lat1 = center.lat() / 57.2958; 
    var lon1 = center.lng() / 57.2958;
    var lat2 = ne.lat() / 57.2958;
    var lon2 = ne.lng() / 57.2958;
    
    // distance = circle radius from center to Northeast corner of bounds
    var dis = r * Math.acos(Math.sin(lat1) * Math.sin(lat2) + 
      Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1));
    

    在玩了更多之后,我注意到map.getBounds() 将包含完整的地图视口。但是,如果您的LatLngBounds 是通过扩展以包含LatLng 点来构建的,然后您发出map.fitBounds(bounds),则该api 会稍微增加地图的视口,以便bounds“框”有一些填充。

    如果您使用地图的当前视口,从视口中心到角落的半径可能比您想要的更长。可能是从视口中心到最远视口边缘中间的距离。 (如果地图不是一个完美的正方形)

    【讨论】:

    • Awesomesauce,正是我所需要的。谢谢!
    • 要让这个例子正常工作,前 3 行应该是 var bounds = map.getBounds(); var center = bounds.getCenter(); var ne = bounds.getNorthEast();
    • 这对我不起作用。我尝试实现这一点,但我不断收到“无法读取未定义的属性'getCenter'”。 'getBounds' 函数有变化吗?
    【解决方案2】:

    上面使用google.maps.geometry.spherical命名空间的Eric's answer的重构版本(确保您加载了Geometry library以使其工作)。

    var bounds = map.getBounds();
    var center = map.getCenter();
    if (bounds && center) {
      var ne = bounds.getNorthEast();
      // Calculate radius (in meters).
      var radius = google.maps.geometry.spherical.computeDistanceBetween(center, ne);
    }
    

    【讨论】:

    • 完美!帮助我保持较低的地方请求不会太快达到配额。
    【解决方案3】:

    我还重构了 Eric 的答案,我的答案是一个独立的函数,并以米为单位返回结果(根据 Places API search 的需要。

    function getBoundsRadius(bounds){
      // r = radius of the earth in km
      var r = 6378.8
      // degrees to radians (divide by 57.2958)
      var ne_lat = bounds.getNorthEast().lat() / 57.2958
      var ne_lng = bounds.getNorthEast().lng() / 57.2958
      var c_lat = bounds.getCenter().lat() / 57.2958
      var c_lng = bounds.getCenter().lng() / 57.2958
      // distance = circle radius from center to Northeast corner of bounds
      var r_km = r * Math.acos(
        Math.sin(c_lat) * Math.sin(ne_lat) + 
        Math.cos(c_lat) * Math.cos(ne_lat) * Math.cos(ne_lng - c_lng)
        )
      return r_km *1000 // radius in meters
    }
    

    【讨论】:

    • 请添加“;”在您的行尾,因此您也可以在缩小版本中使用它
    • @kaiser 分号在 JavaScript 中是可选的,如果缩小软件还不错,则不需要。
    • 这是考虑在视口中不可见的某些区域的半径,如果我必须找到当前视图的半径,那么圆应该只在当前视图中。我该怎么办?
    【解决方案4】:

    我相信Bounds 有 getNorthEast() 和 getSouthWest() 方法,但这会给你一个矩形(实际上是边界),你可以计算这两者之间的距离,等等。 要计算出那个矩形的圆可能有点工作......

    也许这会有所帮助:http://code.google.com/apis/maps/articles/mvcfun.html

    示例:http://code.google.com/apis/maps/articles/mvcfun/step6.html

    【讨论】:

    • 您可以,也许,看到这不是首选和批准的答案。不要指望我保持最新;-)
    猜你喜欢
    • 2017-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-24
    • 2011-09-07
    相关资源
    最近更新 更多