“可视"的半径谷歌地图 v3 中的区域 [英] Radius of "viewable" region in google maps v3

查看:18
本文介绍了“可视"的半径谷歌地图 v3 中的区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道如何在谷歌地图 API v3 中检索可视缩放级别的半径.

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

例如,如果我处于缩放级别 3,并且根据用户屏幕尺寸(假设为 400x400 可视区域),我如何获得可视区域的半径"圆.

For example, if I am at zoom level 3, and depending on the users screen size (lets just say 400x400 viewable region) how do I get the "radius" circle of the viewable area.

或者,我目前正在为我添加到地图的所有点使用 map.fitBounds(),所以实际上我需要就是所有边界的半径.我想要的是诸如20 英里"之类的东西,我可以将其输入到我的数据库应用程序中.

Alternatively I'm currently using map.fitBounds() for all the points I've added to the map so really all I NEED is the radius of all the bounds. What I want is something like "20 miles" that I can feed into my database app.

推荐答案

半径等于从边界中心到边界角之一的距离.使用 本页计算 中的大圆距离公式,我想出了以下内容:

The radius would equal the distance from the center of the bounds to one of the bound's corners. Using the Great Circle Distance Formula from the calculations from this page, I came up with the following:

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 框"有一些填充.

After playing some more with this, I've noticed that map.getBounds() will contain the full map viewport. But if your LatLngBounds is built by extending to include LatLng points, and then you issue a map.fitBounds(bounds), the api increases the map's viewport a bit so that the bounds "box" has some padding.

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

If you use the map's current viewport, the radius from the center to the corner of the viewport might be a longer radius than you want. Maybe the distance from the viewport center to the middle of the furthest viewport edge. (If the map isn't a perfect square)

这篇关于“可视"的半径谷歌地图 v3 中的区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆