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

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

问题描述

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

例如,如果我处于缩放级别3,并且取决于用户的屏幕尺寸(让我们只说400x400可视区域)如何获得可视区域的半径圆圈。



另外,我现在使用map.fitBounds()来处理所有添加到地图中的点,所以我真的需要所有边界的半径。我想要的是类似于20英里的东西,我可以将它馈送到我的数据库应用程序中。

解决方案

从边界的中心到边界的一个角落。使用本页面的计算中的大圆距离公式,我提出了以下内容:

  var bounds = map.getBounds(); 

var center = bounds.getCenter();
var ne = bounds.getNorthEast();

// r =法定英里的地球半径
var r = 3963.0;

//将lat或lng从十进制度转换为弧度(除以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;

//距离=从中心到东北角的圆弧半径
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 box有一些填充。



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


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 size (lets just say 400x400 viewable region) how do I get the "radius" circle of the viewable area.

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));

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天全站免登陆