我如何知道 Lat,Lng 点是否包含在圆内?

发布于 2024-10-08 15:26:29 字数 378 浏览 9 评论 0原文

好的,非常不言自明。我正在使用谷歌地图,我试图找出纬度、经度点是否在半径为 x 的圆内(x 由用户选择)。

边界框对此不起作用。我已经尝试使用以下代码:

distlatLng = new google.maps.LatLng(dist.latlng[0],dist.latlng[1]);
var latLngBounds = circle.getBounds();
if(latLngBounds.contains(distlatLng)){
      dropPins(distlatLng,dist.f_addr);
}

这仍然会导致标记位于圆圈之外。

我猜这是一些简单的数学,需要计算曲率或面积,但我不知道从哪里开始。有什么建议吗?

Ok pretty self explanatory. I'm using google maps and I'm trying to find out if a lat,long point is within a circle of radius say x (x is chosen by the user).

Bounding box will not work for this. I have already tried using the following code:

distlatLng = new google.maps.LatLng(dist.latlng[0],dist.latlng[1]);
var latLngBounds = circle.getBounds();
if(latLngBounds.contains(distlatLng)){
      dropPins(distlatLng,dist.f_addr);
}

This still results in markers being places outside the circle.

I'm guess this is some simple maths requiring the calculation of the curvature or an area but I'm not sure where to begin. Any suggestions?

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

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

发布评论

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

评论(5

另类 2024-10-15 15:26:29

不幸的是,毕达哥拉斯在球体上没有帮助。因此斯图尔特·比尔德的答案是不正确的;经度差与米没有固定的比率,而是取决于纬度。

正确的方法是使用大圆距离公式。假设地球是球形的,一个很好的近似是这样的(在 C++ 中):

/** Find the great-circle distance in metres, assuming a spherical earth, between two lat-long points in degrees. */
inline double GreatCircleDistanceInMeters(double aLong1,double aLat1,double aLong2,double aLat2)
    {
    aLong1 *= KDegreesToRadiansDouble;
    aLat1 *= KDegreesToRadiansDouble;
    aLong2 *= KDegreesToRadiansDouble;
    aLat2 *= KDegreesToRadiansDouble;
    double cos_angle = sin(aLat1) * sin(aLat2) + cos(aLat1) * cos(aLat2) * cos(aLong2 - aLong1);

    /*
    Inaccurate trig functions can cause cos_angle to be a tiny amount
    greater than 1 if the two positions are very close. That in turn causes
    acos to give a domain error and return the special floating point value
    -1.#IND000000000000, meaning 'indefinite'. Observed on VS2008 on 64-bit Windows.
    */
    if (cos_angle >= 1)
        return 0;

    double angle = acos(cos_angle);
    return angle * KEquatorialRadiusInMetres;
    }

其中

const double KPiDouble = 3.141592654;
const double KDegreesToRadiansDouble = KPiDouble / 180.0;

/**
A constant to convert radians to metres for the Mercator and other projections.
It is the semi-major axis (equatorial radius) used by the WGS 84 datum (see http://en.wikipedia.org/wiki/WGS84).
*/
const int32 KEquatorialRadiusInMetres = 6378137;

Unfortunately Pythagoras is no help on a sphere. Thus Stuart Beard's answer is incorrect; longitude differences don't have a fixed ratio to metres but depend on the latitude.

The correct way is to use the formula for great circle distances. A good approximation, assuming a spherical earth, is this (in C++):

/** Find the great-circle distance in metres, assuming a spherical earth, between two lat-long points in degrees. */
inline double GreatCircleDistanceInMeters(double aLong1,double aLat1,double aLong2,double aLat2)
    {
    aLong1 *= KDegreesToRadiansDouble;
    aLat1 *= KDegreesToRadiansDouble;
    aLong2 *= KDegreesToRadiansDouble;
    aLat2 *= KDegreesToRadiansDouble;
    double cos_angle = sin(aLat1) * sin(aLat2) + cos(aLat1) * cos(aLat2) * cos(aLong2 - aLong1);

    /*
    Inaccurate trig functions can cause cos_angle to be a tiny amount
    greater than 1 if the two positions are very close. That in turn causes
    acos to give a domain error and return the special floating point value
    -1.#IND000000000000, meaning 'indefinite'. Observed on VS2008 on 64-bit Windows.
    */
    if (cos_angle >= 1)
        return 0;

    double angle = acos(cos_angle);
    return angle * KEquatorialRadiusInMetres;
    }

where

const double KPiDouble = 3.141592654;
const double KDegreesToRadiansDouble = KPiDouble / 180.0;

and

/**
A constant to convert radians to metres for the Mercator and other projections.
It is the semi-major axis (equatorial radius) used by the WGS 84 datum (see http://en.wikipedia.org/wiki/WGS84).
*/
const int32 KEquatorialRadiusInMetres = 6378137;
甚是思念 2024-10-15 15:26:29

使用 Google Maps API 几何库计算圆心与标记之间的距离,然后将其与半径进行比较。

var pointIsInsideCircle = google.maps.geometry.spherical.computeDistanceBetween(circle.getCenter(), point) <= circle.getRadius();

Use Google Maps API geometry library to calculate distance between circle's center and your marker, and then compare it with your radius.

var pointIsInsideCircle = google.maps.geometry.spherical.computeDistanceBetween(circle.getCenter(), point) <= circle.getRadius();
她如夕阳 2024-10-15 15:26:29

这很简单。您只需计算中心和给定点之间的距离并将其与半径进行比较。您可以从此处获取计算两个纬度之间距离的帮助

It's very simple. You just have to calculate distance between centre and given point and compare it to radius. You can Get Help to calculate distance between two lat lang from here

魔法少女 2024-10-15 15:26:29

以下代码适用于我:我的标记无法拖动到圆外,而是仅悬挂在其边缘(向任何方向),并保留最后一个有效位置。

该函数是标记“拖动”事件的事件处理程序。

_markerDragged : function() {
    var latLng = this.marker.getPosition();
    var center = this.circle.getCenter();
    var radius = this.circle.getRadius();
    if (this.circleBounds.contains(latLng) &&
        (google.maps.geometry.spherical.computeDistanceBetween(latLng, center) <= radius)) {
        this.lastMarkerPos = latLng;
        this._geocodePosition(latLng);
    } else {
        // Prevent dragging marker outside circle
        // see (comments of) http://unserkaiser.com/code/google-maps-marker-check-if-in-circle/
        // see http://www.mvjantzen.com/blog/?p=3190 and source code of http://mvjantzen.com/cabi/trips4q2012.html
        this.marker.setPosition(this.lastMarkerPos);
    }
},

感谢 http://unserkaiser.com/code/google-地图标记检查如果在圆圈/
http://www.mvjantzen.com/blog/?p=3190

The following code works for me: my marker cannot be dragged outside the circle, instead it just hangs at its edge (in any direction) and the last valid position is preserved.

The function is the eventhandler for the markers 'drag' event.

_markerDragged : function() {
    var latLng = this.marker.getPosition();
    var center = this.circle.getCenter();
    var radius = this.circle.getRadius();
    if (this.circleBounds.contains(latLng) &&
        (google.maps.geometry.spherical.computeDistanceBetween(latLng, center) <= radius)) {
        this.lastMarkerPos = latLng;
        this._geocodePosition(latLng);
    } else {
        // Prevent dragging marker outside circle
        // see (comments of) http://unserkaiser.com/code/google-maps-marker-check-if-in-circle/
        // see http://www.mvjantzen.com/blog/?p=3190 and source code of http://mvjantzen.com/cabi/trips4q2012.html
        this.marker.setPosition(this.lastMarkerPos);
    }
},

Thanks to http://unserkaiser.com/code/google-maps-marker-check-if-in-circle/
and http://www.mvjantzen.com/blog/?p=3190 .

奶气 2024-10-15 15:26:29

我确实有点傻了。思考一下我们可以利用毕达哥拉斯定理。

我们有距某个点的最大距离(X 英里)、两个纬度和两个经度。如果我们用这些形成一个三角形,那么我们就可以求解到该点的距离。

假设我们知道坐标 lat1,lng1point1 是圆心,坐标 lat2,lng2point2 > 是我们试图确定的点是否在圆内。

我们使用由 point1point2 确定的点形成一个直角三角形。 point3 将具有坐标 lat1,lng2lat2,lng1(无论是哪一个)。然后我们计算差异(或者如果您愿意)距离 - latDiff = lat2-lat1lngDiff = lng2-lng1

然后我们使用毕达哥拉斯计算距中心的距离 - <代码>dist=sqrt(lngDiff^2+latDiff^2)。

我们必须将所有内容转换为米,以便它能够在 google 地图上正常工作,因此英里乘以 1609(大约),纬度/经度乘以 111000(大约)。这并不完全准确,但它做得足够了。

希望一切都有意义。

I've been a bit silly really. Thinking about it we can use Pythagorus' theorem.

We have a maximum distance away from a point (X miles), and two latitudes and two longitudes. If we form a triangle using these then we can solve for the distance from the point.

So say we know point1 with coordinates lat1,lng1 is the center of the circle and point2 with coordinates lat2,lng2 is the point we are trying to decide is in the circle or not.

We form a right angled triangle using a point determined by point1 and point2. This, point3 would have coordinates lat1,lng2 or lat2,lng1 (it doesn't matter which). We then calculate the differences (or if you prefer) distances - latDiff = lat2-lat1 and lngDiff = lng2-lng1

we then calculate the distance from the center using Pythagorus - dist=sqrt(lngDiff^2+latDiff^2).

We have to translate everything into meters so that it works correctly with google maps so miles are multiplied by 1609 (approx) and degrees of latitude/longitude by 111000 (approx). This isn't exactly accurate but it does an adequate job.

Hope that all makes sense.

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