15

好的,不言自明。我正在使用谷歌地图,我试图找出一个经纬度点是否在半径为 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);
}

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

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

4

6 回答 6

16

带有可拖动中心标记的工作解决方案

你试过contains吗?看看LatLngBounds构造函数。

我写了一篇关于它的文章,其中包含一个指向有效的JSFiddle.net 示例的链接。

jsFiddle 截图


更新版本

于 2011-12-16T07:31:54.173 回答
10

不幸的是,毕达哥拉斯对球体没有帮助。因此 Stuart Beard 的回答是不正确的;经度差异与米没有固定的比例,而是取决于纬度。

正确的方法是使用大圆距离公式。假设一个球形地球,一个很好的近似值是这样的(在 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;
于 2012-09-12T08:03:30.427 回答
5

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

var pointIsInsideCircle = google.maps.geometry.spherical.computeDistanceBetween(circle.getCenter(), point) <= circle.getRadius();
于 2014-06-16T01:49:25.803 回答
5

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

于 2015-12-09T06:18:50.020 回答
4

以下代码对我有用:我的标记不能被拖到圆圈外,而是挂在它的边缘(在任何方向)并保留最后一个有效位置。

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

_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-maps-marker-check-if-in-circle/http://www.mvjantzen.com/blog/?p=3190

于 2013-04-12T17:24:18.720 回答
1

我真的有点傻。考虑一下,我们可以使用毕达哥拉斯定理。

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

所以说我们知道point1坐标lat1,lng1是圆的中心,point2坐标lat2,lng2是我们试图确定的点是否在圆中。

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

然后我们使用 Pythagorus - 计算到中心的距离dist=sqrt(lngDiff^2+latDiff^2)

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

希望一切都说得通。

于 2010-12-18T13:04:04.697 回答