谷歌地图。如何根据中心点的坐标创建 LatLngBounds 矩形(正方形)

发布于 2024-11-02 09:35:33 字数 178 浏览 1 评论 0原文

我有一个点 (X,Y),我想创建一个正方形,Google 地图 LatLngBounds 对象,以便使地理编码请求仅偏向此 LatLngBound 区域。

如何创建这样一个以给定点为中心的 LatLngBounds 正方形?我必须找到NE和SW点。但是给定距离 d 和点 (x,y) 我怎样才能找到它呢?

谢谢

I have a point (X,Y) and I want to create a square , Google maps LatLngBounds object so to make geocode requests bias only into this LatLngBound region.

How can I create such a LatLngBounds square with center the given point? I have to find the NE and SW point. But how can I find it given a distance d and a point (x,y)?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绾颜 2024-11-09 09:35:34

嗯,这非常复杂。对于一个粗略的盒子,试试这个:

if (typeof(Number.prototype.toRad) === "undefined") {
  Number.prototype.toRad = function() {
    return this * Math.PI / 180;
  }
}

if (typeof(Number.prototype.toDeg) === "undefined") {
  Number.prototype.toDeg = function() {
    return this * 180 / Math.PI;
  }
}

var dest = function(lat,lng,brng, dist) {
    this._radius = 6371;
    dist = typeof(dist) == 'number' ? dist : typeof(dist) == 'string' && dist.trim() != '' ? +dist : NaN;
    dist = dist / this._radius;
    brng = brng.toRad();  
    var lat1 = lat.toRad(),
        lon1 = lng.toRad();

    var lat2 = Math.asin(Math.sin(lat1) * Math.cos(dist) + Math.cos(lat1) * Math.sin(dist) * Math.cos(brng));
    var lon2 = lon1 + Math.atan2(Math.sin(brng) * Math.sin(dist) * Math.cos(lat1), Math.cos(dist) - Math.sin(lat1) * Math.sin(lat2));
    lon2 = (lon2 + 3 * Math.PI) % (2 * Math.PI) - Math.PI;
    return (lat2.toDeg() + ' ' +  lon2.toDeg());
}

    var northEastCorner = dest(centreLAT,centreLNG,45,10);
    var southWestCorner = dest(centreLAT,centreLNG,225,10);

编辑

上面是我在 2011 年写的时候他们的做法。如今,谷歌地图 API 已经取得了长足的进步。 @wprater 的答案更加简洁,并且使用了一些较新的 api 方法。

well that's very complicated. for a rough box try this:

if (typeof(Number.prototype.toRad) === "undefined") {
  Number.prototype.toRad = function() {
    return this * Math.PI / 180;
  }
}

if (typeof(Number.prototype.toDeg) === "undefined") {
  Number.prototype.toDeg = function() {
    return this * 180 / Math.PI;
  }
}

var dest = function(lat,lng,brng, dist) {
    this._radius = 6371;
    dist = typeof(dist) == 'number' ? dist : typeof(dist) == 'string' && dist.trim() != '' ? +dist : NaN;
    dist = dist / this._radius;
    brng = brng.toRad();  
    var lat1 = lat.toRad(),
        lon1 = lng.toRad();

    var lat2 = Math.asin(Math.sin(lat1) * Math.cos(dist) + Math.cos(lat1) * Math.sin(dist) * Math.cos(brng));
    var lon2 = lon1 + Math.atan2(Math.sin(brng) * Math.sin(dist) * Math.cos(lat1), Math.cos(dist) - Math.sin(lat1) * Math.sin(lat2));
    lon2 = (lon2 + 3 * Math.PI) % (2 * Math.PI) - Math.PI;
    return (lat2.toDeg() + ' ' +  lon2.toDeg());
}

    var northEastCorner = dest(centreLAT,centreLNG,45,10);
    var southWestCorner = dest(centreLAT,centreLNG,225,10);

EDIT

The above was they way to do it way back in 2011 when I wrote it. These days the google maps api has come on a loooong way. The answer by @wprater is much neater and uses some of the newer api methods.

暮光沉寂 2024-11-09 09:35:34

简单地在 x/y 位置上添加/减去 d/2 不行吗?

给定 x,y 作为中心点:

NW = x-(d/2),y-(d/2)
SE = x+(d/2),y+(d/2)

不过,不要相信我——我数学很糟糕:)

这假设 d 是“直径”,而不是半径。如果“d”是半径,则不必理会除以二的部分。

Wouldn't it work to simply add/subtract d/2 to your x/y locations?

Given x,y as the center point:

NW = x-(d/2),y-(d/2)
SE = x+(d/2),y+(d/2)

Don't trust me on this, though - I am terrible at math :)

This assumes d as a "diameter", rather than a radius. If "d" is the radius, don't bother with the divide-by-two part.

掩于岁月 2024-11-09 09:35:33

您还可以从定义为圆的半径中 getBounds 并将三角函数留给 google。

new google.maps.Circle({center: latLng, radius: radius}).getBounds();

You can also getBounds from a radius defined as a circle and leave the trig to google.

new google.maps.Circle({center: latLng, radius: radius}).getBounds();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文