Radius of “viewable” region in google maps v3

前端 未结 4 1742
渐次进展
渐次进展 2020-12-22 22:41

I need to know how to retrieve the radius of the viewable zoom level in google maps API v3.

For example, if I am at zoom level 3, and depending on the users screen s

4条回答
  •  没有蜡笔的小新
    2020-12-22 23:09

    I also refactored Eric's answer, mine is as a standalone function, and am returning the result in meters (as needed by the 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
    }
    

提交回复
热议问题